Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140847

How to pass an argument to a url in GraphQL

$
0
0

I was wondering how I could pass in a argument into the url in the Root Query. In the resolve funtion the url is https://finnhub.io/api/v1/quote?symbol=AAPL&token=gh4o7. However this will only return the information for AAPL I was wondering how I could make the url like https://finnhub.io/api/v1/quote?symbol={symbol}&token=gh4o7. And grab that {symbol} from the args being passed into the resolve function. Also what would that query look like in graphiql to pass in that variable and get a response back say for TSLA? I'm pretty new to GraphQL so thank you in advance

const graphql = require("graphql");
const axios = require("axios");

const { GraphQLObjectType, GraphQLArray, GraphQLInt, GraphQLString, GraphQLFloat, GraphQLList, GraphQLSchema, Gra } = graphql;

const PriceType = new GraphQLObjectType({
    name: "Price",
    fields: () => ({
        c: { type: GraphQLFloat },
        h: { type: GraphQLFloat },
        l: { type: GraphQLFloat },
        o: { type: GraphQLFloat }
    })
});

const RootQuery = new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
        quote: {
            type: PriceType,
            symbol: GraphQLString,
            resolve(parent, args){
                console.log(args)
//I would like to be able to change this URL based on what I pass into GraphQL as a variable instead of having it hard-coded to always return info on AAPL
                return axios.get(`https://finnhub.io/api/v1/quote?symbol=AAPL&token=gh4o7`)
                    .then(res => res.data)
            }
        }
    }
})

module.exports = new GraphQLSchema({
    query: RootQuery
}) 

//My query looks like this
{
  quote{
    c,
    h,
    l,
    o
  }
}
//And the response looks like this
{"data": {"quote": {"c": 313.04998779296875,"h": 320.45001220703125,"l": 310.5,"o": 318.6199951171875
    }
  }
}

Viewing all articles
Browse latest Browse all 140847

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>