如何从 graphql 查询和突变字段中获取 args 打字稿类型?

How do I get args typescript types from graphql query and mutation fields?

我在使用 typscript 和 graphql 时遇到了一些问题。我似乎无法输入所有内容。
如何在根查询和变异字段中获取类型化参数和父属性?

例如: 服务器:

export interface IContext {
  res: Response;
  req: Request;
  prisma: PrismaClient;
}

const schema = new GraphQLSchema({
    query,
});

  const server = new ApolloServer({
    schema,
    context: ({ req, res }: { req: Request; res: Response }) => ({
      req,
      res,
      prisma,
    }),
  });

根查询:

const query = new GraphQLObjectType<any, IContext>({
  name: 'QueryType',
  fields: {
    notifications: {
      type: new GraphQLList(NotificationType),
      args: { filter: { type: FindManyNotificationArgs } },
      resolve: async (parent, args, { req, prisma }) => {
        // parent is any 
        // args is [argName: string]: any;
        //req and prisma are typed only due to IContext interface at root query
        try {
          isAuth(req);
          return await prisma.notification.findMany({ ...args.filter });
        } catch (error) {
          throw new Error(error);
        }
      },
    },
    messages: {
      type: new GraphQLList(MessageType),
      args: { filter: { type: FindManyMessageArgs } },
      resolve: async (parent, args, { req, prisma }) => {
        // parent is any 
        // args is [argName: string]: any;
        //req and prisma are typed only due to IContext interface at root query
        try {
          isAuth(req);
          return await prisma.message.findMany({ ...args.filter });
        } catch (error) {
          throw new Error(error);
        }
      },
    },
  },
});

消息类型:

export const MessageType = new GraphQLObjectType<Message, IContext>({
  name: 'Message',
  fields: () => ({
    id: { type: GraphQLID },
    ...
    content: { type: GraphQLString },
    hasAttachment: { type: GraphQLBoolean },
    created: { type: GraphQLDateTime },
    updated: { type: GraphQLDateTime },
    expired: { type: GraphQLDateTime },
  }),
});

export const FindManyMessageArgs = new GraphQLInputObjectType({
  name: 'FindManyMessageArgs',
  fields: () => ({
    where: { type: MessageWhereInput },
    orderBy: { type: MessageOrderByInput },
    take: { type: GraphQLInt },
    skip: { type: GraphQLInt },
  }),
});

export const MessageWhereInput: GraphQLInputObjectType =
  new GraphQLInputObjectType({
    name: 'MessageWhereInput',
    fields: () => ({
      id: { type: GraphQLID },
      personId: { type: GraphQLInt },
      person: { type: PersonWhereInput },
      personDocumentStatusId: { type: GraphQLInt },
      personDocumentStatus: { type: PersonDocumentStatusWhereInput },
      content: { type: StringFilter },
      created: { type: GraphQLDateTime },
      updated: { type: GraphQLDateTime },
      expired: { type: GraphQLDateTime },
      AND: { type: MessageWhereInput },
      OR: { type: GraphQLList(MessageWhereInput) },
      NOT: { type: MessageWhereInput },
    }),
  });

如何正确定义 typescript 类型以从我定义的类型中获取 rootquery 和 mutationquery 中键入的所有内容

喜欢来自

type: new GraphQLList(NotificationType), (This should be parent in resolve function)
args: { filter: { type: FindManyNotificationArgs } },) (This should be args in resolve function)

谢谢!

事实证明,为了安全起见,args 不能自动输入,因为 args 可以是任何东西。