无法与Primsa一起在GraphQL-yoga上使用片段 [英] Unable to use Fragments on GraphQL-yoga with Primsa

查看:176
本文介绍了无法与Primsa一起在GraphQL-yoga上使用片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有qq和prisma-bindings的graphql-瑜伽

I am using graphql-yoga with Prisma and Prisma-Bindings

我正在尝试向我的解析器添加一个片段,以便在用户要求自定义字段costToDate时始终获取特定字段(在这种情况下为id).

I'm trying to add a fragment to my resolver so that a specific field (id in this situation) is always fetched when the user asks for a custom field, costsToDate.

因此,我可以进行一些其他查询来构建该字段的结果,并且我需要该对象的ID.

This is so i can make some additional queries needed to build the result for that field, and i need the ID of the object for that.

不幸的是,我似乎无法使其正常工作,而且有关graphql-yoga和Prisma的细节方面,文档似乎有些欠缺.

Unfortunatley i can't seem to get it to work, and the documentations seems a little lacking on the specifics with graphql-yoga and Prisma.

这是类型的定义:

type Job {
    id: ID!
    projectNumber: String!
    client: Client!
    name: String!
    description: String
    quoteNumber: String
    workshopDaysQuoted: String!
    quoted: String!
    targetSpend: String!
    costs: [JobCost!]!
    estimatedCompletion: DateTime
    completed: Boolean!
    costTotal: String
    workshopDaysUsed: String
    costsToDate: String
}

这是查询的解析器:

const jobs = {
    fragment: `fragment description on Jobs { id }`,
    resolve: jobsResolver
}

async function jobsResolver(root, args, context, info) {
    await validatePermission(args,context,info,['admin','user','appAuth'])
    const {showCompleted} = args
    const completedFilter = (typeof showCompleted === 'boolean') ? { completed: showCompleted } : {}
    const jobIDS = await context.db.query.jobs({ where: completedFilter }, `{ id }`)
    //console.log(jobIDS);
    let jobs = await context.db.query.jobs({
        where: completedFilter
    }, info)
    return  await getAllJobCostsToDateList(jobs)
}

我正在按照以下方式应用fragmentReplacements.

I am applying the the fragmentReplacements as per below.

const fragmentReplacements = extractFragmentReplacements(resolvers)

console.log(fragmentReplacements)

const port = process.env.PORT || 3010

const graphQLServer = new GraphQLServer({
    typeDefs: './src/schema.graphql',
    resolvers,
    resolverValidationOptions: {
        requireResolversForResolveType: false
    },
    context: req => ({
        ...req,
        db: new Prisma({
            typeDefs: `src/generated/prisma.graphql`,
            fragmentReplacements,
            endpoint: PRISMA_ENDPOINT,
            secret: PRISMA_KEY,
            debug: false
        })
    })
})

如果我用console.log记录fragmentReplacements对象,则会得到以下内容,因此它似乎确实是在拾取碎片.

If i console.log the fragmentReplacements object i get the following, so it does seem to be picking up the fragments.

[ { field: 'job', fragment: 'fragment costsToDate on Job { id }' },
  { field: 'jobs',
    fragment: 'fragment costsToDate on Jobs { id }' } ]

所以我在这里的期望是,如果我对作业或要求查询costToDate字段的作业进行查询,那么它也会获取该作业/每个作业的ID.

So my expectation here is that if i make a query against jobs or job that asks for the costsToDate field that it will also fetch the id for the job/each job.

但是,如果我进行以下查询.

However if i make the following query.

query{
  jobs{
    description
    costsToDate
  }
}

但是我看不到任何id,在解析程序功能的root参数中也没有任何内容.

But i see no id fetched, and nothing in the root parameter on the resolver function.

道歉,因为我可能在这里完全弄错了树,这似乎是一个简单的要求,但是我不能完全解决.当然,我缺少基本的东西.

Apologies as i am probably barking up completely the wrong tree here, seems like a somewhat simple requirement, but i can't quite work it out. Sure i'm missing something fundamental.

谢谢!

Gareth

推荐答案

片段用于始终给定类型检索给定字段 >.

它遵循以下格式:

fragment NameOfYourFragment on YourType { ... }

您当前无法有条件地应用给定的片段,因为它始终会被应用. 此外,您在Jobs上指定了一个片段,但Prisma使用的类型名称为Job(即使您具有jobjobs解析器)

You currently can't apply a given fragment conditionally as it is always applied. Moreover, you specified a fragment on Jobs, but the type name used by Prisma is Job (even if you have the job and jobs resolvers)

您可能只需要以下解析器:

You probably only need the following resolver:

const job = {
  fragment: `fragment JobId on Job { id }`,
  resolve: jobsResolver
}

这篇关于无法与Primsa一起在GraphQL-yoga上使用片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆