mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
Follow-up on previous commit.
This commit is contained in:
parent
e2c2e200f4
commit
d0b24a2381
3 changed files with 10 additions and 84 deletions
|
@ -12,20 +12,16 @@ sidebar_categories:
|
||||||
- whats-new
|
- whats-new
|
||||||
Essentials:
|
Essentials:
|
||||||
- essentials/building-schema
|
- essentials/building-schema
|
||||||
- essentials/queries
|
- essentials/running-a-server
|
||||||
- essentials/mutations
|
- essentials/fetching-data
|
||||||
Schema Development:
|
Schema Development:
|
||||||
- schemas/organization
|
- schemas/organization
|
||||||
- schemas/types
|
- schemas/types
|
||||||
- schemas/directives
|
- schemas/directives
|
||||||
- schemas/resolvers
|
- schemas/resolvers
|
||||||
- schemas/context
|
- schemas/context
|
||||||
Running a Server:
|
TODO:
|
||||||
- server/index
|
|
||||||
- server/middleware
|
|
||||||
- server/queries
|
|
||||||
- server/engine
|
- server/engine
|
||||||
- server/secrets
|
|
||||||
Best Practices:
|
Best Practices:
|
||||||
- best-practices/authentication
|
- best-practices/authentication
|
||||||
- best-practices/permissions
|
- best-practices/permissions
|
||||||
|
@ -38,6 +34,8 @@ sidebar_categories:
|
||||||
- best-practices/versioning
|
- best-practices/versioning
|
||||||
- best-practices/schema-stitching
|
- best-practices/schema-stitching
|
||||||
- best-practices/infrastructure
|
- best-practices/infrastructure
|
||||||
|
- best-practices/testing
|
||||||
|
- best-practices/secrets
|
||||||
Working with Backends:
|
Working with Backends:
|
||||||
- backends/general
|
- backends/general
|
||||||
- backends/sql
|
- backends/sql
|
||||||
|
|
5
docs/source/essentials/fetching-data.md
Normal file
5
docs/source/essentials/fetching-data.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Fetching Data
|
||||||
|
---
|
||||||
|
|
||||||
|
## Fetching Data
|
|
@ -1,77 +0,0 @@
|
||||||
---
|
|
||||||
title: Queries
|
|
||||||
---
|
|
||||||
|
|
||||||
## Prerequisites
|
|
||||||
|
|
||||||
* A basic understanding of a GraphQL schema ([Schema]())
|
|
||||||
## Prerequisites
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
A GraphQL query is for reading data. The schema defines the types of queries which are available to the clients connecting to your server.
|
|
||||||
|
|
||||||
## Material
|
|
||||||
|
|
||||||
* GraphQL query defines the shape of data that will be returned by a particular request
|
|
||||||
* This is what an author + books query looks like coming from the client
|
|
||||||
* make sure it has arguments
|
|
||||||
* This query is then checked again the server's schema
|
|
||||||
* looks like this:
|
|
||||||
* "root" level queries define the main entry points
|
|
||||||
* Each of those root queries returns a type
|
|
||||||
* You have to have a query
|
|
||||||
* It's an entry point like all rest endpoints
|
|
||||||
* It's how you fetch data
|
|
||||||
|
|
||||||
**Actually writing resolvers for your queries is found in server/queries**
|
|
||||||
|
|
||||||
> TODO: The below headings were left over from the other document. Do we want to remove them?
|
|
||||||
|
|
||||||
## Implementing Queries in Apollo Server
|
|
||||||
|
|
||||||
> (Evans) this section feels very similar to resolvers
|
|
||||||
|
|
||||||
Now that we understand the Query type, GraphQL types, and resolvers, we can explain the following code to define our schema and resolvers. This example shows the
|
|
||||||
|
|
||||||
```js
|
|
||||||
const { ApolloServer } = require('apollo-server');
|
|
||||||
|
|
||||||
const typeDefs = `
|
|
||||||
type Process {
|
|
||||||
params: [String]
|
|
||||||
program: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type Query {
|
|
||||||
process: Process
|
|
||||||
argv: [String]
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
// Resolvers define the technique for fetching the types in the
|
|
||||||
// schema. We'll retrieve books from the "books" array above.
|
|
||||||
const resolvers = {
|
|
||||||
Process: {
|
|
||||||
params: (parent) => parent.argv.slice(1)
|
|
||||||
program: (parent) => parent.argv[0]
|
|
||||||
url: (_,_,context) => context.req.baseUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
Query: {
|
|
||||||
process: () => process
|
|
||||||
argv: () => process.argv
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
new ApolloServer({ typeDefs, resolvers, context: { req } })
|
|
||||||
.listen()
|
|
||||||
.then(({ url }) => {
|
|
||||||
console.log(`Visit ${url} to run queries!`);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## Material to include
|
|
||||||
|
|
||||||
* This section ties all of the information in the prereqs to show you how to implement Queries with the Apollo Server
|
|
||||||
* essentially copy and paste code that you can then add onto
|
|
Loading…
Add table
Reference in a new issue