Skip to content

Instantly share code, notes, and snippets.

View SaraVieira's full-sized avatar
🤷‍♀️
open sourcy and shit

Sara Vieira SaraVieira

🤷‍♀️
open sourcy and shit
View GitHub Profile
const { RESTDataSource } = require("apollo-datasource-rest");
class RandomUser extends RESTDataSource {
constructor() {
super();
this.baseURL = "https://randomuser.me/api/";
}
}
module.exports = RandomUser;
const typeDefs = gql`
type Name {
title: String,
first: String,
last: String,
}
type Location {
street: String
city: String
const typeDefs = gql`
scalar Image
type Query {
image: Image,
notImage: Image
}
`;
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"
}
};
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");
};
const Image = new GraphQLScalarType({
name: "Image",
description: "An Image Scalar",
serialize: value => isImage(value)
});
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
const { ApolloServer, gql } = require("apollo-server");
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
image: String,
notImage: String
}
`;
"scripts": {
"start": "nodemon index.js localhost 4000"
},
npm i apollo-server graphql nodemon
# yarn add apollo-server graphql nodemon