├── LICENSE.txt ├── README.md ├── client ├── app.js └── index.html └── server ├── .gitignore ├── package.json ├── server-code-first.js └── server.js /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2018-2023 Mirko Nasato 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GraphQL Hello World 2 | 3 | This is a project used in the [GraphQL by Example](https://www.udemy.com/course/graphql-by-example/?referralCode=7ACEB04674F000BAC061) course. 4 | 5 | It uses Apollo Server to expose a minimal GraphQL API, and a web page with vanilla JavaScript as the client. 6 | -------------------------------------------------------------------------------- /client/app.js: -------------------------------------------------------------------------------- 1 | async function fetchGreeting() { 2 | const response = await fetch('http://localhost:9000/', { 3 | method: 'POST', 4 | headers: { 5 | 'Content-Type': 'application/json', 6 | }, 7 | body: JSON.stringify({ 8 | query: 'query { greeting }', 9 | }), 10 | }); 11 | const { data } = await response.json(); 12 | return data.greeting; 13 | } 14 | 15 | fetchGreeting().then((greeting) => { 16 | document.getElementById('greeting').textContent = greeting; 17 | }); 18 | -------------------------------------------------------------------------------- /client/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 |12 | The server says: 13 | 14 | Loading... 15 | 16 |
17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /server/.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world-server", 3 | "private": true, 4 | "type": "module", 5 | "dependencies": { 6 | "@apollo/server": "^4.6.0", 7 | "graphql": "^16.6.0", 8 | "nexus": "^1.3.0" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /server/server-code-first.js: -------------------------------------------------------------------------------- 1 | import { ApolloServer } from '@apollo/server'; 2 | import { startStandaloneServer } from '@apollo/server/standalone'; 3 | import { makeSchema, queryType } from 'nexus'; 4 | 5 | const Query = queryType({ 6 | definition: (t) => { 7 | t.string('greeting', { 8 | resolve: () => 'Hello world!', 9 | }); 10 | }, 11 | }); 12 | 13 | const schema = makeSchema({ types: [Query] }); 14 | 15 | const server = new ApolloServer({ schema }); 16 | const { url } = await startStandaloneServer(server, { listen: { port: 9000 } }); 17 | console.log(`Server running at ${url}`); 18 | -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | import { ApolloServer } from '@apollo/server'; 2 | import { startStandaloneServer } from '@apollo/server/standalone'; 3 | 4 | const typeDefs = `#graphql 5 | type Query { 6 | greeting: String 7 | } 8 | `; 9 | 10 | const resolvers = { 11 | Query: { 12 | greeting: () => 'Hello world!', 13 | }, 14 | }; 15 | 16 | const server = new ApolloServer({ typeDefs, resolvers }); 17 | const { url } = await startStandaloneServer(server, { listen: { port: 9000 } }); 18 | console.log(`Server running at ${url}`); 19 | --------------------------------------------------------------------------------