aaronwlee / oak-graphql

A simple graphql middleware for oak deno framework.
Other
121 stars 23 forks source link

Q&A: Need help for relationship and foreign key use in resolver #44

Closed borsemayur2 closed 3 years ago

borsemayur2 commented 3 years ago

How can i add user object in the resolver while resolving book query

const data = {
  todos: [
    { id: 1, title: "todo1", user: 1 },

    { id: 2, title: "todo2", user: 2 },
  ],
  users: [
    { id: 1, name: "ABC" },
    { id: 2, name: "XYZ" },
  ],
};

const types = gql`
  type User {
    id: Int!
    name: String! 
    todos: [Todo!]!
  }

  type Todo {
    id: Int!
    title: String!
    user: User!
  }

  type Query {
    todos: [Todo!]!
    todo(id: Int): Todo!
    users: [User!]!
    user(id: Int): User!
  }
`;

const resolvers = {
  Query: {
    todos: (parent: any, args: any, context: any, info: any) => {
      return data.todos ;
    },
    todo: (parent: any, args: any, context: any, info: any) => {
      return data.todos[args.id - 1];
    },
    users: (parent: any, args: any, context: any, info: any) => {
      return data.users;
    },
    user: (parent: any, args: any, context: any, info: any) => {
      return data.users[args.id - 1];
    },
  },
}

image

borsemayur2 commented 3 years ago

I've posted in issues as there is no tab for discussions