graphql 使用GraphQLObjectType定义schema,定义单项数据和列表时ts报错

新手上路,请多包涵

问题描述

graphql 使用GraphQLObjectType定义schema,定义列表时ts类型检查正常,同时定义单项数据和列表时ts报错

问题出现的环境背景及自己尝试过哪些方法

应该是args字段的问题,尝试都加入id、type字段,不报类型检测错误了,但数据库查不到

相关代码

models/articles.ts

import {
      GraphQLObjectType,
      GraphQLID,
      GraphQLString,
      GraphQLInt,
      GraphQLNonNull,
      GraphQLList
} from 'graphql'

import mysql from '../../../utils/database'

const ArticleType = new GraphQLObjectType({
      name: 'Article',
      fields: {
            id: { type: GraphQLID },
            author: { type: GraphQLString },
            title: { type: GraphQLString },
            image_url: { type: GraphQLString },
            summary: { type: GraphQLString },
            content: { type: GraphQLString },
            type: { type: GraphQLInt },
            sort: { type: GraphQLInt },
            tags: { type: GraphQLString },
            view_count: { type: GraphQLInt },
            created_at: { type: GraphQLString },
            updated_at: { type: GraphQLString }
      }
})

//当定义了更具id查询article之后类型检查就报错了
const article = {
      type: ArticleType,
      args: {
            id: { type: new GraphQLNonNull(GraphQLID) }
      },
      resolve: async (_, { id }) => await mysql.Query(`
            select * from mc_news
            where id=${id} 
      `)
}
//当定义了更具id查询article之后类型检查就报错了

const articles = {
      type: new GraphQLList(ArticleType),
      args: {
            type: { type: GraphQLInt },
            page: { type: GraphQLInt },
            pageSize: { type: GraphQLInt },
      },
      resolve: async (_, { page = 0, pageSize = 10, type }) => await mysql.Query(`
            select * from mc_news
            where type=${type}
            limit ${((page - 1) < 0 ? 0 : (page - 1)) * pageSize},${pageSize}
      `)
}

export { article, articles }

schema.ts

import { GraphQLSchema, GraphQLObjectType } from 'graphql'
import { article, articles } from './models/articles'

export default new GraphQLSchema({
      query: new GraphQLObjectType({
            name: 'Queries',
            fields: {
                  article,
                  articles
            }
      })
})

报错信息:

Type '{ article: { type: GraphQLObjectType<any, any, { \[key: string\]: any; }>;

args: { id: { type: GraphQLScalarType; }; };

resolve: (\_: any, { id }: { id: any; }) => Promise<unknown>; };

articles: { ...; }; }' is not assignable to type 'Thunk<GraphQLFieldConfigMap<any, any, { id: any; }>>'.

  

Type '{ article: { type: GraphQLObjectType<any, any, { \[key: string\]: any; }>;

args: { id: { type: GraphQLScalarType; }; };

resolve: (\_: any, { id }: { id: any; }) => Promise<unknown>; };

articles: { ...; }; }' is not assignable to type 'GraphQLFieldConfigMap<any, any, { id: any; }>'.

  

Property 'articles' is incompatible with index signature.

Type '{ type: GraphQLList<GraphQLType>;

args: { type: { type: GraphQLScalarType; };

page: { type: GraphQLScalarType; };

pageSize: { ...; }; };

resolve: (\_: any, { page, pageSize, type }: { ...; }) => Promise<...>; }' is not assignable to type 'GraphQLFieldConfig<any, any, { id: any; }>'.

Types of property 'resolve' are incompatible.

  

Type '(\_: any, { page, pageSize, type }: { page?: number; pageSize?: number; type: any; }) => Promise<unknown>' is not assignable to type 'GraphQLFieldResolver<any, any, { id: any; }>'.

Types of parameters '\_\_1' and 'args' are incompatible.

Property 'type' is missing in type '{ id: any; }' but required in type '{ page?: number; pageSize?: number; type: any; }'.

你期待的结果是什么?实际看到的错误信息又是什么?

希望熟悉graphql以及typescript的大神解释一下为什么报错,以及解决方案。

阅读 2.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题