swarthout / feathers-apollo

Feathers and Apollo Server Sample Project
MIT License
176 stars 20 forks source link

Feathers Auk update #18

Open lobosan opened 7 years ago

lobosan commented 7 years ago

Hi, I was wondering if you could please update the example to work with the latest version of FeathersJS and maybe add a frontend built on top of this starter kit, that would be super helpful.

Thanks in advance for your help

swarthout commented 7 years ago

Yeah I can do that. I'm currently studying for finals at university, but in a week I'll have time to work on it. In the mean time, I always review pull requests and I would appreciate some suggestions and an update on what has changed in the new release. Thanks!

lobosan commented 7 years ago

Hey that sounds good, in the meantime you can have a look at this resource.

Good luck with your finals

swarthout commented 7 years ago

Started working on these updates. Can you please review my changes as I go and make sure I'm on the right track? Would appreciate it. The PR is #19

lobosan commented 7 years ago

Hi Scott, sure I'll have a look during this week. Thanks again for sharing your knowledge

nacmonad commented 6 years ago

bump

swarthout commented 6 years ago

I appreciate your persistence. I played around with this last week and I'm getting closer to getting this all upgraded. If you have progress please commit to the auk-upgrade branch. Also if you are interested in helping maintain this project let me know.

idealley commented 6 years ago

Actually I made this code with Auk version of feathers. it does not require changes... provided your feathers auk version is working (auth etc.)

if this is the case, then just do the following:

  1. create a folder graphql
  2. create the following files
    • graphql.schema.js
    • graphql.resolvers.js
    • graphql.service.js
  3. as before add in `src/services/index.js as the last service:
    • app.configure(graphql);

The code of your schema and resolvers do not change.

      // Resolver example
      users(root, args, context) {
        return Users
          .find(context)
          .then(results => results.data);
      },

The graphql.service.js should be looking like:

// Initializes the `graphql` service on path `/graphql`

const graphql = require('graphql-server-express').graphqlExpress;
const graphiql = require('graphql-server-express').graphiqlExpress;
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
// const addMockFunctionsToSchema = require('graphql-tools').addMockFunctionsToSchema;

const Resolvers = require('./graphql.resolvers');
const Schema = require('./graphql.schema');

module.exports = function () {
  const app = this;
  // const paginate = app.get('paginate');

  const executableSchema = makeExecutableSchema({
    typeDefs: Schema,
    resolvers: Resolvers.call(app)
  });

  // const options = {
  //   name: 'graphql',
  //   paginate
  // };

  // Initialize our service with any options it requires
  app.use('/graphql', graphql((req) => {
    const {token, provider} = req.feathers;
    return {
      schema: executableSchema,
      context: {
        token,
        provider
      }
    };
  }));

  app.use('/graphiql', graphiql({
    endpointURL: '/graphql',
  }));
};

the "magic" here is to retrieve the token and add it to the context variable, thus if your authentication hooks are set on the services you resolve the requests will not go through and return an error. Thus you do not need the viewer service as from the token you can "populate" the user from the db.

swarthout commented 6 years ago

That is awesome! Can you submit a PR? There is some work I started on a feature branch that you could take a look at for auth.

3kynox commented 6 years ago

Hello, joining this issue.

I'm trying to update the example with latest AWK but I'm new to the subject and I get pain :)

@idealley I see no more usage of apollo package on your example, is it intentional ? Can I continue use it ? What's the advantages ? Any guidelines ?

Disapointing thing too is about you said No need to update schema and resolvers and at the end you talk about "magic" where it is said we no more need viewer service. But in schema and resolver files, we use the viewer multiple times. So finally there is changes to these files ?

I'm trying to do it myself and get back to here if it's done.

Thanks.

idealley commented 6 years ago

Hello,

You can check this example: https://github.com/EuropeanRespiratorySociety/api-ers

Actually graphql-express-server uses Apollo server as a dependency.

The graphql service is based on what I have written above.

Let me know if you need help.

Sam

3kynox commented 6 years ago

Thanks a lot @idealley !

I have all the required materials to achieve what I am doing now.

Exploring this.