Skip to content

Commit

Permalink
Updated to integrate uploading deeper, and allowing async context to …
Browse files Browse the repository at this point in the history
…be provided
  • Loading branch information
theodorDiaconu committed Aug 24, 2018
1 parent bfbea8d commit 3f54a40
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 76 deletions.
7 changes: 6 additions & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 0.7.0

- Built-in support for Uploads
- Moved `schemaDirectives` and `context` to the main Apollo options and they are blended in properly

# 0.6.0

Morpher has changed the way it receives inputs, being serialised via JSON.
Morpher has changed the way it receives inputs, being serialised via EJSON.

# 0.5.0

Expand Down
3 changes: 2 additions & 1 deletion __tests__/client.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './morpher/client';
import './accounts/client';
import './accounts/client';
import './default/client';
23 changes: 23 additions & 0 deletions __tests__/default/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import client, { wsLink } from '../apolloClient';
import gql from 'graphql-tag';
import { loginWithPassword, logout } from 'meteor-apollo-accounts';
import { resolve } from 'dns';

const PASSWORD = '12345';

describe('Default', () => {
it('Should allow me to provide custom context', async () => {
const response = await client.query({
query: gql`
query {
secretContextMessage
}
`,
});

assert.equal(
response.data.secretContextMessage,
'SECRET_MESSAGE_IN_CONTEXT'
);
});
});
16 changes: 16 additions & 0 deletions __tests__/default/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { load } from 'meteor/cultofcoders:apollo';

load({
typeDefs: `
type Query {
secretContextMessage: String!
}
`,
resolvers: {
Query: {
secretContextMessage(_, args, { secretMessage }) {
return secretMessage;
},
},
},
});
7 changes: 6 additions & 1 deletion __tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ import { initialize } from 'meteor/cultofcoders:apollo';
import './graphql/init';
import './morpher/server';
import './accounts/server';
import './default/server';

initialize();
initialize({
context: async () => ({
secretMessage: 'SECRET_MESSAGE_IN_CONTEXT',
}),
});
2 changes: 1 addition & 1 deletion client/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
DISABLE_WEBSOCKETS: false,
disableWebsockets: false,
getLink: link => link,
};
9 changes: 7 additions & 2 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { split, concat, ApolloLink } from 'apollo-link';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { getMainDefinition } from 'apollo-utilities';
import { meteorAccountsLink } from './meteorAccountsLink';
import { createUploadLink } from 'apollo-upload-client';
import Config from './config';
import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions';

checkNpmVersions({
'subscriptions-transport-ws': '0.9.x',
'apollo-live-client': '0.2.x',
'apollo-upload-client': '8.x.x',
'apollo-client': '2.x.x',
'apollo-cache-inmemory': '1.x.x',
'apollo-link': '1.x.x',
'apollo-link-http': '1.x.x',
'apollo-link-ws': '1.x.x',
'apollo-morpher': '0.1.x',
'apollo-morpher': '0.2.x',
});

import {
Expand Down Expand Up @@ -50,8 +52,10 @@ export function initialize(config = {}) {
uri: GRAPHQL_ENDPOINT,
});

const uploadLink = createUploadLink();

if (meteorAccountsLink) {
links.push(concat(meteorAccountsLink, httpLink));
links.push(concat(meteorAccountsLink, httpLink, uploadLink));
} else {
links.push(httpLink);
}
Expand All @@ -75,6 +79,7 @@ export function initialize(config = {}) {
link,
wsLink,
httpLink,
uploadLink,
};
}

Expand Down
4 changes: 2 additions & 2 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ Initialize accepts as an argument a configuration object:

```js
initialize({
DISABLE_WEBSOCKETS: false;
})
disableWebsockets: false,
});
```

- [Read more about React Apollo](https://www.apollographql.com/docs/react/)
Expand Down
23 changes: 12 additions & 11 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ Do not override `schema` and `context`, use `graphql-load` and `MeteorApolloOpti
## `MeteorApolloOptions`
```js
initialize({}, {
// Context that is going to be passed to resolvers
// By default we inject { db } from 'meteor/cultofcoders:grapher' package
// If you override this to include other context information, make sure to include that as well
context: {
db // Access to database context via Grapher
},
initialize({
// Here you can provide the apollo options provided here:
// https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#constructor-options-lt-ApolloServer-gt

// You must not override schema

// You can add `schemaDirectives` and `context` without worrying about context update
schemaDirectives: [MyCustomDirective],
context: async () => ({
services
})
}, {
// This is just an example, you have cors built in ApolloOptions
// You can also add other connect middlewares to '/graphql' endpoint
middlewares: [],
Expand All @@ -46,14 +50,11 @@ initialize({}, {
// So basically the `gui` config from `ApolloConstructorOptions` will be ignored
gui: Meteor.isDevelopment

// Load custom schema directives that you want to use
schemaDirectives: []

// Because we support authentication by default
// We inject { user, userId } into the context
// These fields represent what fields to retrieve from the logged in user on every request
// You can use `undefined` if you want all fields
userDefaultFields: {
userFields: {
_id: 1,
username: 1,
emails: 1,
Expand Down
1 change: 0 additions & 1 deletion server/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
let Config = {
context: {},
userFields: {
_id: 1,
username: 1,
Expand Down
62 changes: 34 additions & 28 deletions server/initialize.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Meteor } from 'meteor/meteor';
import { db } from 'meteor/cultofcoders:grapher';
import { WebApp } from 'meteor/webapp';

import { addMockFunctionsToSchema } from 'graphql-tools';
import { ApolloServer } from 'apollo-server-express';

import { getSchema } from 'graphql-load';
import { AUTH_TOKEN_KEY } from '../constants';
import { getExecutableSchema } from './schema';
import defaultSchemaDirectives from './directives';
import { getUserForContext } from './core/users';
import Config from './config';
import { resolve } from 'path';

/**
*
Expand All @@ -21,8 +17,6 @@ export default function initialize(apolloConfig = {}, meteorApolloConfig = {}) {
{
gui: Meteor.isDevelopment,
middlewares: [],
schemaDirectives: [],
context: { db },
userDefaultFields: {
_id: 1,
roles: 1,
Expand All @@ -33,24 +27,30 @@ export default function initialize(apolloConfig = {}, meteorApolloConfig = {}) {
meteorApolloConfig
);

const schema = getExecutableSchema(meteorApolloConfig);
const { typeDefs, resolvers } = getSchema();

apolloConfig = Object.assign(
{
schema,
introspection: Meteor.isDevelopment,
debug: Meteor.isDevelopment,
path: '/graphql',
formatError: e => ({
message: e.message,
locations: e.locations,
path: e.path,
}),
context: getContextCreator(meteorApolloConfig),
subscriptions: getSubscriptionConfig(meteorApolloConfig),
const initialApolloConfig = Object.assign({}, apolloConfig);
apolloConfig = {
...initialApolloConfig,
typeDefs,
resolvers,
schemaDirectives: {
...defaultSchemaDirectives,
...(initialApolloConfig.schemaDirectives
? initialApolloConfig.schemaDirectives
: []),
},
apolloConfig
);
introspection: Meteor.isDevelopment,
debug: Meteor.isDevelopment,
path: '/graphql',
formatError: e => ({
message: e.message,
locations: e.locations,
path: e.path,
}),
context: getContextCreator(meteorApolloConfig, initialApolloConfig.context),
subscriptions: getSubscriptionConfig(meteorApolloConfig),
};

const server = new ApolloServer(apolloConfig);

Expand All @@ -74,11 +74,17 @@ export default function initialize(apolloConfig = {}, meteorApolloConfig = {}) {
});
}

function getContextCreator(meteorApolloConfig) {
function getContextCreator(meteorApolloConfig, defaultContextResolver) {
return async function getContext({ req, connection }) {
const defaultContext = defaultContextResolver
? await defaultContextResolver()
: {};

Object.assign(defaultContext, { db });

if (connection) {
return {
...meteorApolloConfig.context,
...defaultContext,
...connection.context,
};
} else {
Expand All @@ -92,7 +98,7 @@ function getContextCreator(meteorApolloConfig) {
}

return {
...meteorApolloConfig.context,
...defaultContext,
...userContext,
};
}
Expand All @@ -108,7 +114,7 @@ function getSubscriptionConfig(meteorApolloConfig) {
if (loginToken) {
const userContext = getUserForContext(
loginToken,
meteorApolloConfig.userDefaultFields
meteorApolloConfig.userFields
).then(userContext => {
resolve(userContext);
});
Expand Down
28 changes: 0 additions & 28 deletions server/schema.js

This file was deleted.

0 comments on commit 3f54a40

Please sign in to comment.