0

I am looking to do something similar to what is done in the graphql tutorial: https://graphql.org/learn/queries/#arguments

this is my schema

type Query{
        author(id:Int!):author
        authors:[author]
        books:[book]
        book(id:Int!):book
    }

    type author{
        id:Int!
        name:String
        surname: String
    }
    enum currency{
        EUR
        US
        }

    type book{
        id:Int!
        title:String!
        authors:[author]
        published:String
        price(unit: currency = EUR):Float
    }

    schema{
        query:Query
    }

i want to convert the Currency, but i dont know how i connect my function with this returned type, to return the converted value.

Full server.js

const express=require('express');
const axios = require('axios');
const express_graphql = require('express-graphql');
var {buildSchema} = require('graphql');


var schema = buildSchema(`
    type Query{
        author(id:Int!):author
        authors:[author]
        books:[book]
        book(id:Int!):book
    }

    type author{
        id:Int!
        name:String
        surname: String
    }
    enum currency{
        EUR
        US
        }

    type book{
        id:Int!
        title:String!
        authors:[author]
        published:String
        price(unit: currency = EUR):Float
    }

    schema{
        query:Query
    }
`)

var getAuthors = function(args){
    return axios.get('http://localhost:1234/Authors').then(res => res.data);
}

var getAuthor = function(args){
    return axios.get('http://localhost:1234/Authors/'+args.id).then(res => res.data);
}

var getBooks = function(args){
    return axios.get('http://localhost:4321/Books').then(res => res.data);
}

var getBook = function(args) {
    return axios.get('http://localhost:4321/Books/'+args.id).then(res => res.data);
}

var root = {
    author:getAuthor,
    authors:getAuthors,
    books:getBooks,
    book:getBook
}

const app=express();
app.use('/graphql', express_graphql({
    schema,
    rootValue: root,
    graphiql: true
}));

function convertCurrency(Eur, unit){
   if(unit==="EUR"){
       return Eur;
   }

   if(Unit === "US"){
       return Eur * 1.11;
   }
}

app.listen(8080, ()=>{
    console.log('server is running on port 8080..')
})
Eddi
  • 1
  • 1

1 Answers1

0

You need to provide a resolver for the price field, that would look something like this:

(parent, args) => convertCurrency(parent.price, args.unit)

Unfortunately, buildSchema does not allow you to create a fully-featured schema. Normally, you would define a resolve function (or resolver) for any field you wanted to provide resolution logic for, but buildSchema creates a schema without any resolvers. Passing in fake resolvers through the root value is a bit of hackery that tries to get around this problem, but it has it's limits.

You have two options:

  • Create your schema programmatically, by constructing the GraphQLSchema object yourself.
  • Continue using Schema Definition Language (SDL) to define your schema, but switch to using makeExecutableSchema from graphql-tools
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183