我遇到与此线程Unable to use Fragments on GraphQL-yoga with Primsa
相同的问题但是,该解决方案对我不起作用。
我正试图限制用户使用片段来访问其他电子邮件。如果通过查询发送ID,则可以看到我的电子邮件,但看不到其他电子邮件。但是,如果我不发送ID,则不会收到任何电子邮件。
我认为我的所有配置都很好,或者,我太盲目看不到出了什么问题。有人能为我提供正在发生的事情的线索吗?
我正在使用 “ graphql”:“ ^ 14.3.1”, “ graphql-cli”:“ ^ 3.0.11”, “ graphql-yoga”:“ ^ 1.17.4”, 和prisma 1.34
我的用户解析器是这个
import getUserId from '../utils/getUserId'
const User = {
email: {
fragment: 'fragment userId on User { id }',
resolve(parent, args, {
request
}, info) {
const userId = getUserId(request, false)
console.log(userId) //here I can see the id of the logged user
console.log(parent.id) // here I see UNDEFINED
if (userId && userId === parent.id) {
return parent.email
}
return null
}
}
}
export {
User as
default
}
我的server.js:
import {
GraphQLServer,
PubSub
} from 'graphql-yoga'
import {
resolvers,
fragmentReplacements
} from './resolvers/index'
import prisma from './prisma'
const pubsub = new PubSub()
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
context(request) {
return {
pubsub,
prisma,
request
}
},
fragmentReplacements
})
console.log(fragmentReplacements) //Here I can see that the application is picking up the fragments
export {
server as
default
}
最后,我的用户查询:
prisma
}, info) {
const opArgs = {
first: args.first,
skip: args.skip,
after: args.after,
orderBy: args.orderBy
}
if (args.query) {
opArgs.where = {
OR: [{
name_contains: args.query
}]
}
}
return prisma.query.users(opArgs, info)
},```