Comments (1)
Let's say you want to use GitHub's REST API for exposing Gists via your GraphQL API.
For that, you start by creating GistType.js
that lists all the fields that you want to expose to your API users. See schema/StoryType.js
, schema/CommentType.js
as an example.
Then you can add a "data loader" that will fetch a Gist from GitHub by it's ID, you can add it to src/Context.js
:
gistById = new DataLoader(ids =>
ids.map(id =>
fetch(`https://api.github.com/gists/${id}`, {/* options */}).then(x => x.json())
);
Then update GraphQL Node interface that will allow fetching gists by IDs via "node(id)" field (src/schema/Node.js
):
const { nodeInterface, nodeField: node, nodesField: nodes } = nodeDefinitions(
(globalId, context) => {
const { type, id } = fromGlobalId(globalId);
switch (type) {
...
case 'Gist':
return context.gistById.load(id).then(assignType('Gist'));
default:
return null;
}
},
obj => {
switch (getType(obj)) {
...
case 'Gist':
return require('./GistType').default;
default:
return null;
}
},
);
Now you should be able to fetch a Gist by ID by using this query:
query {
node(id: "2e404fe4409ca617a4b6948ea3a67e40") {
... on Gist {
id url description
}
}
}
Then you can add "gists" top-level field that fetches multiple gists at once:
{
type: new GraphQLList(GistType),
resolve(root, args, context) {
return fetch('https://api.github.com/gists', {*/ options */})
.then(x => x.json())
.then(gists => {
// Optionally cache them in "gistById" data loader
gists.forEach(gist => context.gistById.prime(gist.id, gist));
return gists;
});
}
}
So you could fetch them like this:
query {
gists {
id url description
}
}
from relay-starter-kit.
Related Issues (20)
- Adding controller/service layer for business logic HOT 3
- MongoDB instead of PostgreSQL HOT 1
- Where is the Passport.js implementation? HOT 1
- Dependabot can't resolve your JavaScript dependency files
- File Upload HOT 3
- Dependabot can't resolve your JavaScript dependency files
- Dependabot can't resolve your JavaScript dependency files
- big int HOT 1
- SyntaxError: Unexpected token = HOT 2
- Missing quotation marks in setup.js HOT 2
- Feature Request: "New Feature Walkthrough"
- Add the flexibility of multiple roles HOT 1
- Bug with emails that have "." in them breaks username requirements
- README.md out of date - no "setup" script HOT 3
- Nightly build fails
- Account settings page does not update
- docker image for the repo HOT 1
- error while run yarn setup HOT 1
- Readme.md has not been updated. HOT 1
- app-deploy.js file does not exists
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from relay-starter-kit.