const { ApolloServer, gql } = require("apollo-server");
const faker = require("faker");
const typeDefs = gql`
type Dog {
id: ID!
name: String!
age: Int
}
type Query {
allDog: [Dog!]!
}
`;
const mocks = {
ID: () => faker.random.uuid(),
Int: () => faker.random.number({ min: 1, max: 25 }),
String: () => faker.name.firstName(),
};
const resolvers = {
Query: {
allDogs: () => [{id: 1, name: "Meatball"}]
}
};
const server = new ApolloServer({typeDefs, resolvers, mocks, mockEntireSchema: false});
server
.listen()
.then(({ port }) => console.log(`Server running on port ${port}`));
var query = `{ allBooks { title } }`;
var url = "https://books.com/graphql";
var opts = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query })
};
fetch(url, opts)
.then(res => res.json())
.then(
({ data }) => `
<p>Book of the day: ${data.Book.title}</p>
`
)
.then(text => (document.body.innerHTML = text))
.catch(console.error);
import { request } from 'graphql-request'
var url = 'https://books.com/graphql'
var mutation = `
mutation newBook($title: String! $author: String!) {
addBook(title: $title, author: $author) {
title
author
}
}
`
var variables = { title: 'Jane Eyre', author: 'Charlotte Brontë' }
request(url, mutation, variables)
.then(console.log)
.catch(console.error)