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

发布于 2022-09-12 00:36:34 字数 3790 浏览 23 评论 0

问题描述

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的大神解释一下为什么报错,以及解决方案。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文