hasura / client-side-graphql

147 stars 13 forks source link

possible to add to context? #1

Open veeramarni opened 5 years ago

veeramarni commented 5 years ago

Similar to graphql server, can we add something to the context which could be used for resolver.

For example

// resolvers -> get where on earth id -> get consolidated_weather data and return
const resolvers = {
  Query: {
    cityWeather: (root, args, { weatherCtx } , info) => {
      return weatherCtx.getWoeid(args.city_name).then( (response) => {
        if (!response) {
          return null;
        }
        return weatherCtx.getWeather(response).then( (weather) => {
          if (!weather) {
            return null;
          }
          let consolidated_weather = weather.consolidated_weather;
          // check for args applicable_date to apply filter
          consolidated_weather = args.applicable_date ? consolidated_weather.find(item => item.applicable_date === args.applicable_date) : consolidated_weather[0];
          const respObj = {'temp': consolidated_weather.the_temp.toString(), 'min_temp': consolidated_weather.min_temp.toString(), 'max_temp': consolidated_weather.max_temp.toString(), 'city_name': weather.title, 'applicable_date': consolidated_weather.applicable_date};
          return respObj;
        });
      });
    }
  },
};
Fubinator commented 3 years ago

I'm currently facing the same problem. I'd like to inject some context into the client's query function and work with that inside the resolver.

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

  const client = new ApolloClient({
    link: new SchemaLink({ schema }),
    cache: new InMemoryCache(),
  });

  const result = await client.query({ query: qry, context: "hallo" });

And the resolver:

    Query: {
      item: (root: any, args: any, context: any, info: any) => {
        console.log("ctx", context);
        return {};
      }
  }

Executing this logs undefined.

@wawhal sorry for pinging, but do you have any idea?