Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

javascript - Cannot fetch data in Graphql Relay

I'm learning Relay to use in a React-Relay project. After my research and learning on the internet, I've run into problems with my graphql schema and resolvers. I can't seem to figure out what resolvers do I need and how go about it correctly. If someone can point me in the right direction, that would be great. I have attached my code below. All I'm trying to do is fetch elements of a list using the relay graphql server specification.

Graphql Schema

interface Node {
    id: ID!
}

type Link implements Node {
    id: ID!
    title: String!
    description: String!
}

type LinkConnection {
    edges: [LinkEdge]
    pageInfo: PageInfo!
}

type LinkEdge {
    cursor: String!
    node: Link
}

type Query {
    links(after: String, before: String, first: Int, last: Int): LinkConnection
    node(id: ID!): Node
}

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  startCursor: String
  endCursor: String
}

Resolvers

const resolvers = {
    Query,
    Node: {
        __resolveType(node, context, info){
            if(node.title){
                return 'Link'
            }
            return null
        }
    },
}


Query Resolver

const node = async (parent, args, {prisma}) => {
    try{
        console.log('hit')
        const data = await prisma.link.findUnique({
            where: {
                id: Number(args.id)
            }
        })
        console.log(data)
        return data
    } catch(err){
        return err
    }
}

export default {
    node,
}

P.S. Im using Apollo Server and Prisma under the hood

Edit:

I solved this issue by realizing that the resolves are invoked in the same order the schema is nested. So by writing separate resolvers for each type and passing the information in parent argument, things worked.

question from:https://stackoverflow.com/questions/65918579/cannot-fetch-data-in-graphql-relay

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...