smooth-code / graphql-directive

Use custom directives in your GraphQL schema and queries 🎩
MIT License
152 stars 14 forks source link

Implementing a live directive #9

Open eljenso opened 6 years ago

eljenso commented 6 years ago

I'm trying to implement a live directive, which keeps the caller of the query up-to-date. This is my current code, which does just resolve() with the directive.

const typeDefs = /* GraphQL */ `
  directive @live on FIELD | FIELD_DEFINITION

  type Todo {
    text: String!
    completed: Boolean!
  }

  type Query {
    allTodos: [Todo]! @live
  }
`;

const resolvers = {
  Query: {
    allTodos() {
      return someTodoArray;
    }
  }
};

const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers
});

addDirectiveResolveFunctionsToSchema(executableSchema, {
  live(resolve) {
    return resolve();
  }
});

How would I push new results to the caller with this approach? Is there a way to get a reference to the caller to push new results?

giautm commented 6 years ago

@eljenso : Hi, please trying to use new feature in graphql-tools, https://www.apollographql.com/docs/graphql-tools/schema-directives.html

eljenso commented 6 years ago

@giautm Thanks for the hint, will have a look.