This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { RESTDataSource } = require("apollo-datasource-rest"); | |
class RandomUser extends RESTDataSource { | |
constructor() { | |
super(); | |
this.baseURL = "https://randomuser.me/api/"; | |
} | |
} | |
module.exports = RandomUser; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const typeDefs = gql` | |
type Name { | |
title: String, | |
first: String, | |
last: String, | |
} | |
type Location { | |
street: String | |
city: String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const typeDefs = gql` | |
scalar Image | |
type Query { | |
image: Image, | |
notImage: Image | |
} | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const resolvers = { | |
Image: Image, | |
Query: { | |
image: () => | |
"https://uploads.codesandbox.io/uploads/user/8d35d7c1-eecb-4aad-87b0-c22d30d12081/l2nh-cat.jpeg", | |
notImage: () => "https://codesandbox.io/s/4qlo54l7k9" | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isImage = value => { | |
if (typeof value !== "string") throw new GraphQLError("Value passed is not a string"); | |
const extension = path.extname(value).slice(1); | |
if (imageExtensions.includes(extension)) return value; | |
throw new GraphQLError("Value passed is not an Image"); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Image = new GraphQLScalarType({ | |
name: "Image", | |
description: "An Image Scalar", | |
serialize: value => isImage(value) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const imageExtensions = require("image-extensions"); | |
const isImage = value => { | |
if (typeof value !== "string") return null; | |
const extension = path.extname(value).slice(1); | |
if (imageExtensions.includes(extension)) return value; | |
return null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ApolloServer, gql } = require("apollo-server"); | |
// Construct a schema, using GraphQL schema language | |
const typeDefs = gql` | |
type Query { | |
image: String, | |
notImage: String | |
} | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
"start": "nodemon index.js localhost 4000" | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm i apollo-server graphql nodemon | |
# yarn add apollo-server graphql nodemon |