Reputation: 1789
I have a GraphQL server using ExpressJS. This is the schema using GraphQL-Tools:
import UserModel from '../models/User'
export const Schema = `
type User {
id: Int!
firstName: String
lastName: String
}
type Query {
users: [User]
}
schema {
query: Query
}
`
export const resolveFunctions = {
Query: {
users() {
return UserModel.findAll()
}
}
}
Without GraphQL-Tools I'm able to get full list of users and a certain user but now when I'm using GraphQL-Tools I have no idea how to get a certain user by id.
I didn't find any resources online on GraphQL tools except the official documentation, so I hope this will help other people who are finding an answer.
Thank you!
Upvotes: 2
Views: 397
Reputation: 4587
May be you should have a look at graphqly. It's an abstraction built on top of graphql-tools
that help you develop Graphql services easier. Here's the sample code with graphqly
:
import graphly from "graphqly";
const gBuilder = graphly.createBuilder();
// define types, inputs ... (in any order)
gBuilder.type("Products").implements("List").def(`
products: [Product]!
`);
gBuilder.type("Product").def(`
id: ID!
name: String!
link: String
price: Int
`);
gBuilder
.query(`
products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products
`)
.resolve((root, args, context) => {
// your resolver here
});
Upvotes: 1
Reputation: 14395
You should add a query:
user(id: String!): User
and the handler:
user(root, args, ctx) {
return UserModel.findOne({id: args.id});
}
(you might need to adjust the parameter types and the call semantics, I just outlined the required steps).
If you'd like to see a working example, you can have a look at a graphQL starter project. I recently added to it a very similar query.
Upvotes: 4