gpt4 book ai didi

node.js - 错误 : Schema must contain uniquely named types but contains multiple types named "DateTime"

转载 作者:行者123 更新时间:2023-12-04 12:39:38 26 4
gpt4 key购买 nike

我正在尝试生成一个 GraphQL 模式。

我有以下解析器。他们三个在不同的文件中。其中 2 个用于收集数据,而其中一个只是一个触发器 API,用于让它们从数据源收集更多数据。

@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;
}

}

}

生成模式的代码是联合的,如下所示:
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
}
);

当我尝试生成时,我得到的错误是:
Error: Schema must contain uniquely named types but contains multiple types named "DateTime".

不确定它是从哪里得到这个“DateTime”的。

谢谢

最佳答案

如果您使用相同的 name 命名了两个或多个对象类型,通常会发生此错误。 .
例如,

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

const Y = new GraphQLObjectType({
name: 'Hello',
...
});
两者 XY对象类型具有相同的 name这在 GraphQL 中是不允许的。
尝试使用不同的名称。

关于node.js - 错误 : Schema must contain uniquely named types but contains multiple types named "DateTime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60719312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com