解析器
import { Resolvers } from '../generated/ResolversType';
import Query from './Query';
import Mutation from './Mutation';
import InterfacesAndUnions from './InterfacesAndUnions';
import scalars from './scalars';
const resolvers: Resolvers = {
...InterfacesAndUnions,
...scalars,
Query,
Mutation
};
export default resolvers;
突变
import { MutationResolvers } from '../../generated/ResolversType';
import AuthorizedOpenedMutationUnion from './AuthorizedOpenedMutationUnion/AuthorizedOpenedMutationUnion';
const Mutation: MutationResolvers = {
admin: () => {
return null;
},
my: () => {
return null;
},
viewer: AuthorizedOpenedMutationUnion
};
export default Mutation;
授权OpenedMutationUnion
import { MutationResolvers } from '../../../generated/ResolversType';
import AuthorizedMutation from './AuthorizedMutation';
import OpenedMutation from './OpenedMutation';
const AuthorizedOpenedMutationUnion: MutationResolvers['viewer'] = (_: any, __: any, { currentUser }) => {
if (currentUser === null) {
return OpenedMutation;
}
return AuthorizedMutation;
};
export default AuthorizedOpenedMutationUnion;
授权突变
import { Inaction } from '../../../../generated/ResolversType';
const AuthorizedMutation = {
_: () => Inaction.Inaction
};
export default AuthorizedMutation;
***OpenedMutation ***
import { Inaction } from '../../../../generated/ResolversType';
import user from './user';
const OpenedMutation = {
ImplementingUserInterfaces: user,
_: Inaction.Inaction
};
export default OpenedMutation;
用户
import createUser from './resolvers/createUser';
const user = {
createUser
};
export default user;
创建用户
import { ApolloError } from 'apollo-server-express';
import { UserGender } from '../../../../../../../generated/ResolversType';
export default function createUser() {
try {
if (Math.random()) {
return {
lastName: 'asdasd',
firstName: 'asdasd',
nickname: 'asdasd',
email: 'asdasd',
gender: UserGender.Female,
age: 'asdasd',
[Symbol.toPrimitive]: () => 'UnconfirmedUser'
};
}
if (Math.random()) {
return {
lastName: 'asdasd',
firstName: 'asdasd',
nickname: 'asdasd',
email: 'asdasd',
gender: UserGender.Female,
age: 'asdasd',
[Symbol.toPrimitive]: () => 'LetterNotSent'
};
}
return {
lastName: 'asdasd',
firstName: 'asdasd',
nickname: 'asdasd',
email: 'asdasd',
gender: UserGender.Female,
age: 'asdasd',
[Symbol.toPrimitive]: () => 'WarningDuplicateEmail'
};
} catch (error) {
throw new ApolloError('Problem');
}
}
所有代码都可以在这里查看。
这是我第一次使用 typescript 和 graphql-code-generator。
我不知道我应该为 AuthorizedOpenedMutationUnion、AuthorizedMutation、OpenedMutation、user、createUser 生成什么类型的文件。