Comments (4)
@rfgamaral Fortunately, the package apollo-datasource-rest
works as a standalone, you don't need to pair it exactly with Apollo Server to make it work properly.
Here is an example of use combined with graphql-yoga
:
index.js
const { GraphQLServer } = require('graphql-yoga')
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const theAPI = require('./datasources/api');
const server = new GraphQLServer({
typeDefs,
resolvers,
context: () => ({
dataSources: () => {
return {
myAPI: new theAPI(),
};
},
})
})
server.start(() => console.log('Server is running on http://localhost:4000'))
./datasources/api
const { RESTDataSource } = require('apollo-datasource-rest');
class theAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = "https://myexample.com";
this.initialize({}); // calling initialize() function with empty object is the key
}
async getAllData() {
// return all the data
}
}
./resolvers
const resolvers = {
Query: {
hello: (root, args, context) => {
return context.dataSources().myAPI.getAllData();
}
},
};
module.exports = resolvers;
from datasource-rest.
For future reference, initialising HttpCache worked for me:
apollographql/apollo-server#2240 (comment)
from datasource-rest.
Unfortunately, this package does not work as a standalone, you need to pair with Apollo Server to make it work properly (there are workarounds to make it work without Apollo but they are bit cumbersome IMO).
In that same tutorial you linked, you'll eventualy see this:
const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
const { createStore } = require('./utils');
const LaunchAPI = require('./datasources/launch');
const UserAPI = require('./datasources/user');
const store = createStore();
const server = new ApolloServer({
typeDefs,
dataSources: () => ({
launchAPI: new LaunchAPI(),
userAPI: new UserAPI({ store })
})
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Look at the dataSources
in Apollo Server instance, that's the key.
I wish this packaged was a bit more flexible and configurable to work with other servers and not just Apollo. But that's what we have for the moment. This is not a bug, it's a feature.
from datasource-rest.
Sorry for the delay in response. This seems related to rest datasource's requirement of having initialize
called on it.
In v4 of the rest datasource package, this is no longer part of the API.
from datasource-rest.
Related Issues (20)
- Next release of package @apollo/datasource-rest HOT 2
- Type signature for params is too narrow HOT 4
- RESTDataSource’s willSendRequest parameter changes HOT 2
- Get/Set HTTP Status? HOT 1
- willSendRequest this.context does not exist on xxxxDataSource HOT 1
- `didReceiveResponse` hook no longer available, blocking Apollo v4 upgrade HOT 6
- Errors stop getting sent to client if the override of fetch includes a catch callback HOT 2
- Configuring external caching in Apollo v4 HOT 3
- Question related to changes in request parameters HOT 2
- RESTDataSource implementation issue with `willSendRequest` & `@apollo/utils.withrequired` is a devDependency HOT 4
- Apollo Datasource adds content-type: header when Content-Type (case sensitive) header is set HOT 3
- RESTDataSource no longer have access to all datasources through context HOT 2
- Document `contextValue` recipe for circular references in context object
- Posting FormData as body (for file uploads) not possible anymore HOT 7
- Forward all cache options to key value cache HOT 6
- Unable to connect to Localhost server HOT 1
- @apollo/datasource-rest willSendRequest param type missing `agent` HOT 7
- [apollo-datasource-rest] Handling 404 errors HOT 2
- Feature request - be able to tell if API was resolved by memory, cache, or source HOT 3
- RESTDataSource - body based cacheKeyFor POST request HOT 3
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 datasource-rest.