Meteor accounts only in Apollo GraphQL

An implementation of Meteor accounts only in Apollo, for an app I’m making. No websockets, only GraphQL.

And a great example @janikvonrotz did https://github.com/janikvonrotz/meteor-apollo-accounts-example

1 Like

It seems like there’s some open issues on this package that makes it sound like folks may want to hold off using it.

What’s the currently accepted way to use meteor accounts in an apollo app?

Using meteorClientConfig and createMeteorNetworkInterface from http://dev.apollodata.com/core/meteor.html#API?

I’ve cleaned the issues on the repository and I can confirm that everything is working fine. I’m using this package in a lot of apps for a long time and it’s working perfectly in all of them

1 Like

I’m having some trouble getting up and running. This is my server.js whic is a mix of the meteor-starter-kit and meteor-apollo-accounts docs but I can’t seem to get it working. Is ‘meteor/apollo’ clashing with the meteor-apollo-accounts setup?

import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import {loadSchema, getSchema} from 'graphql-loader'
import {initAccounts} from 'meteor/nicolaslopezj:apollo-accounts'
import { Random } from 'meteor/random';

const typeDefs = [
  `
type Email {
  address: String
  verified: Boolean
}

type User {
  emails: [Email]
  randomString: String
  _id: String
}

type Query {
  user: User
}
`,
];

const resolvers = {
  Query: {
    user(root, args, context) {
      /*
       * We access to the current user here thanks to the context. The current
       * user is added to the context thanks to the `meteor/apollo` package.
       */
      return context.user;
    },
  },
  User: {
    emails: ({ emails }) => emails,
    randomString: () => Random.id(),
  },
};

const options = {}
// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts(options)


// Load all your resolvers and type definitions into graphql-loader
loadSchema({typeDefs, resolvers})

// Gets all the resolvers and type definitions loaded in graphql-loader
const schema = getSchema({
  typeDefs,
  resolvers,
})

const executabelSchema = makeExecutableSchema(schema);

createApolloServer({
  executabelSchema,
});

client setup


//top-level imports
import React from 'react';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';
//APOLLO SPECIFIC
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { meteorClientConfig } from 'meteor/apollo';
import { ApolloProvider } from 'react-apollo';
//ROUTES
import AppRoutes  from './routes.js';
import { getLoginToken } from 'meteor-apollo-accounts';

const networkInterface = createNetworkInterface({
  uri: 'http://localhost:3000/graphql',
});

networkInterface.use([{
  applyMiddleware(req, next) {
    if (!req.options.headers) {
      req.options.headers = {};
    }

    getLoginToken()
      .then((token) => {
        req.options.headers.authorization = token;
        next();
      })
      .catch((err) => {
        next();
        throw new Error(JSON.stringify(err));
      });
  },
}]);

const client = new ApolloClient({
  networkInterface,
});



// INJECT MAIN APP
Meteor.startup(() => {
  render(
    <ApolloProvider client={client}>
  		<AppRoutes />
    </ApolloProvider>,
    document.getElementById('app')
  );
});

I have a container wrapped to grab the user. It was working with the vanilla setup from ‘meteor/apollo’ package, but I realized that wouldn’t work in a react-native client (which I’d like to do at some point) so I decided to give meteor-apollo-accounts a try, and now the query doesn’t seem to return anything:



const GET_USER_DATA = gql`
  query getCurrentUser {
    user {
      emails { address, verified },
      randomString,
      _id
    }
  }
`;

export default graphql(GET_USER_DATA)(PublicLayout)

I think its schema the key in createApolloServer

const schema = makeExecutableSchema(getSchema())

createApolloServer({schema})

You’re right… getting tired over here. Thanks for taking a minute to look. The below code works:

import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import { typeDefs, resolvers } from '/imports/api/schema';
import {loadSchema, getSchema} from 'graphql-loader'
import {initAccounts} from 'meteor/nicolaslopezj:apollo-accounts'

const options = {}
// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts(options)


// Load all your resolvers and type definitions into graphql-loader
loadSchema({typeDefs, resolvers})
const loadedSchema = getSchema()
// Gets all the resolvers and type definitions loaded in graphql-loader
const schema = makeExecutableSchema(loadedSchema)

//const executabelSchema = makeExecutableSchema(schema);

createApolloServer({
  schema,
});
//top-level imports
import React from 'react';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';
//APOLLO SPECIFIC
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { meteorClientConfig } from 'meteor/apollo';
import { ApolloProvider } from 'react-apollo';
//ROUTES
import AppRoutes  from './routes.js';
import { getLoginToken } from 'meteor-apollo-accounts';


const client = new ApolloClient(meteorClientConfig());


// INJECT MAIN APP
Meteor.startup(() => {
  render(
    <ApolloProvider client={client}>
  		<AppRoutes />
    </ApolloProvider>,
    document.getElementById('app')
  );
});
2 Likes