# ...
type Item {
# ...
email: String
# Gravatar picture
picture: Picture
# ...
}
# Gravatar picture
type Picture {
link: String
}
# ...
const crypto = require('crypto');
export const resolvers = {
// ...
Item: {
picture: (parent, args, context, info) => {
const pictureHash = crypto.createHash('md5').update(parent.email).digest('hex');
return ({
link: `https://www.gravatar.com/avatar/${pictureHash}`,
})
},
},
// ...
}
subkit request \
--token eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
--url http://localhost:8080/graphql \
--query 'query loadItem {item(id: "mikebild") {id email picture {link}}}'
Benefits of persistent queries:
- Whitelisting GraphQL queries to prevent very complex queries
- Save bandwidth, because only the operationName and variables will submitted
Create a file in your root directory named as your query operation name with .gql
extention (e.g. ./loadItems.gql
).
./loadItems.gql
query loadItems($take: Int) {
items(take: $take) {
id
email
}
}
Run a persistent query using the operation name only.
subkit request -u http://localhost:8080/graphql --operation loadItems --variables '{"take": 10}'