Created
October 8, 2018 03:29
-
-
Save emsifa/b9a6655b442ba0bce4f3169ae356b669 to your computer and use it in GitHub Desktop.
[emsifa.com] GraphQL Mocking dengan Apollo Server
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
// Import dependencies yang dibutuhkan | |
const express = require('express'); | |
const { ApolloServer, gql } = require('apollo-server-express'); | |
// Jabarkan skema graphql | |
const typeDefs = gql` | |
type Query { | |
hello: String | |
users: [User]! | |
user(id: ID!): User | |
} | |
type User { | |
id: ID! | |
name: String! | |
age: Int! | |
email: String! | |
password: String! | |
} | |
`; | |
// Buat resolver untuk setiap types | |
const resolvers = { | |
Query: { | |
hello: () => 'Hello world!', | |
user: (root, args) => { | |
return { | |
id: args.id, | |
name: "John Doe", | |
age: 40, | |
email: "[email protected]", | |
password: "cukupsayadantuhanyangtau", | |
} | |
} | |
}, | |
}; | |
// Inisiasi apollo dan express server | |
const server = new ApolloServer({ | |
typeDefs, | |
resolvers, | |
mocks: { | |
User: () => { | |
return { | |
name: "Muhammad Syifa", | |
age: 26, | |
email: "[email protected]", | |
password: "rahasiadongweee", | |
}; | |
}, | |
}, | |
mockEntireSchema: false, | |
}); | |
const app = express(); | |
server.applyMiddleware({ app }); | |
// Jalankan aplikasi | |
app.listen({ port: 4000 }, () => | |
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment