Unable to use Fragments on GraphQL-yoga with Primsa

最后都变了- 提交于 2019-12-10 23:39:48

问题


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

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.

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.

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.

Here is the definition of the type:

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
}

And here is the resolver for the query:

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)
}

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
        })
    })
})

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 }' } ]

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
  }
}

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.

Thanks!

Gareth


回答1:


A fragment is used to always retrieve given fields on a given type.

It follows the following format:

fragment NameOfYourFragment on YourType { ... }

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
}


来源:https://stackoverflow.com/questions/53427037/unable-to-use-fragments-on-graphql-yoga-with-primsa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!