Error: Schema must contain uniquely named types but contains multiple types named "DateTime"
Asked Answered
B

5

9

I'm trying to generate a GraphQL schema.

I have the following Resolvers. The 3 of them are in different files. 2 of them are for gathering data while one of them is just a trigger API to have them collect more data from a data source.

@Resolver()
export class RefreshDataSchema{
    constructor(
        private readonly aggregatedDataService : AggregateDataService
    ){}

    @Mutation(() => Boolean)
    async refreshTransactions() : Promise<Boolean>{
        Logger.info("Refresh transactions mutation received.");
        return await this.aggregatedDataService.fetchAndAggregateTransactions();
    }

    @Mutation(() => Boolean)
    async refreshAccounts() : Promise<Boolean>{
        Logger.info("Refresh accounts mutation received.");
        return await this.aggregatedDataService.fetchAndAggregateAccounts();
    }
}

@Resolver(() => Account)
export class AccountSchema extends Account{

    @Query(() => Account)
    async getAccount(
        @Arg('query', () => AccountQuery)
        query: AccountQuery,
    ) {
        const data = await Account.findOne({ where: query });
        if (data === undefined) {
            throw new Error(`Account for query "${JSON.stringify(query)}" could not be found!`);
        }
        return data;
    }

    @Query(() => [Account])
    async getAccounts(
        @Arg('query', () => AccountQuery)
        query: AccountQuery,
        @Arg('first', { defaultValue: 10 })
        first: number = 10,
        @Arg('offset', { defaultValue: 0 })
        offset: number = 0,
    ) {
        Logger.info(`GraphQL Query: get accounts. @First: ${first} @Offset: ${offset} received.`);
        const data = Account.find({
            take: first,
            skip: offset,
            order: { balance_in_cents: 'DESC' },
            where: query,
        });

        if(data === undefined){
            throw new Error(`Account for query "${JSON.stringify(query)}" could not be found!`);
        }

        return data;
    }

};

@Resolver(() => Transaction)
export class TransactionSchema extends Transaction{

    @Query(() => [Transaction])
    async getTransactions(
        @Arg('query', () => TransactionQuery)
        query: TransactionQuery,
        @Arg('first', { defaultValue: 10 })
        first: number = 10,
        @Arg('offset', { defaultValue: 0 })
        offset: number = 0,
    ) {
        Logger.info(`GraphQL Query: get transactions. @Query: ${query} @First: ${first} @Offset: ${offset} received.`);
        try{
            let data = await Transaction.find({
                take: first,
                skip: offset,
                order: { executed_on: 'DESC' },
                where: query
            })
            Logger.info("Transaction data retrieved succesfully.");
            return data;
        } catch(err) {
            Logger.error(err);
            return err;
        }

    }

}

And the code to generate the schema, which is federated, looks like this:

import { buildSchemaSync} from 'type-graphql';
import {createResolversMap} from 'type-graphql/dist/utils/createResolversMap';
import {
    TransactionSchema, AccountSchema, RefreshDataSchema
} from '../graphql';
import { printSchema, buildFederatedSchema } from '@apollo/federation';
import { gql } from 'apollo-server-express';

const compiledSchema = buildSchemaSync({
    resolvers: [
        TransactionSchema,
        AccountSchema,
        RefreshDataSchema
    ],
});

export const schema = buildFederatedSchema(
    {
      typeDefs: gql(printSchema(compiledSchema)),
      resolvers: createResolversMap(compiledSchema) as any
    }
  );

When I attempt to generate, the error I get is this:

Error: Schema must contain uniquely named types but contains multiple types named "DateTime".

Not sure where it's getting this "DateTime" from.

Thanks

Bittner answered 17/3, 2020 at 9:13 Comment(0)
K
9

This error usually occurs if you have named two or more of your Object Types with the same name.

For example,

const X = new GraphQLObjectType({
   name: 'Hello',
   ...
});

const Y = new GraphQLObjectType({
   name: 'Hello',
   ...
});

Both X and Y Object Types have the same name which is not allowed in GraphQL. Try using different names.

Kristiankristiansand answered 18/9, 2020 at 21:36 Comment(2)
Welcome to Stack Overflow. I've edited your answer to improve the formatting. Please review the edits so that when you contribute future answers, you know how to format your code for improved readability. For more information on Stack Overflow's formatting, see Markdown help.Elsie
I don't think so this is a problem. Because suppose in the schema can have many ``name` ``` input GitConfigure2FAInput { provider: GitProvider! token: String! remoteRepositoryUrl: String! } input GitConfigureInput { provider: GitProvider! username: String! password: String! remoteRepositoryUrl: String! } ```Concentric
I
7

I know it has been a long time since this has been posted. I have been using NestJS + GraphQL + Prisma and I faced the same issue, but I thought of posting something that helped me, since I spent a hefty amount of time trying to fix it.

In my case deleting the /dist folder. If you want to know about what exactly causes this, you can find it here

Influenza answered 18/5, 2022 at 10:12 Comment(2)
Link don't works anymoreSammysamoan
worked for me. in my case I was renaming an entity and deleting the /dist got me past thisRefugee
H
1

If you got this error by using Graphql + Nestjs + typeorm maybe go to the entity which you got the error, add a name into the @InputType

this issue cz you try to use code first strategy for graphql, the entity by typeorm and InputType by graphql will create schema with same name So, to fix this, give the graphql input type a name, as follow:

@InputType('name', {isAbstract: true}) ...

Huynh answered 13/6, 2022 at 15:19 Comment(1)
Thank you @yue-dong. I was providing a name to the @InputType but adding { isAbstract: true } was necessary to avoid the error.Ecospecies
M
0

This is the official way to create type definitions and a resolver map from your resolver classes:

import { buildTypeDefsAndResolvers } from 'type-graphql';

const { typeDefs, resolvers } = await buildTypeDefsAndResolvers({
  resolvers: [FirstResolver, SecondResolver],
});

This is shown in the docs here.

Mass answered 17/3, 2020 at 12:14 Comment(0)
P
0

There is an issue with Type-Graphql. Try to use "type-graphql": "^0.18.0-beta.10".

Preservative answered 17/4, 2020 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.