2016-02-29 3 views
1

Представьте, что у нас есть схема GraphQL, где некоторые поля зависят от одноранговых полей, которые не разрешены в родительском типе. foo и bar в фрагменте кода ниже.Как получить одноранговые поля в GraphQL?

const MyType = new GraphQLObjectType({ 
    name 'MyType' 
    fields { 
    id: { 
     type: GraphQLString // Resolved from parent type 
    }, 
    foo: { 
     type: GraphQLString, 
     description: 'Foo', 
     resolve: function({id}, args, {rootValue:{logger, datasources:{pool}}}) { 
     // Making some queries to database 
     } 
    }, 
    bar: { 
     type: GraphQLString, 
     description: 'Bar', 
     resolve: function({id}, args, {rootValue:{logger, datasources:{pool}}}) { 
     // Making some queries to database 
     } 
    }, 
    baz: { 
     type: GraphQLString, 
     description: 'Baz', 
     resolve: function({id}, args, {rootValue:{logger, datasources:{pool}}}) { 
     // This field actually depends from 'foo' and 'bar' peers. 
     // How to obtain peer fields 'foo' and 'bar to resolve this field? 
     // We have access to 'id' here, but this field resolved from parent type 
     // and we can't access to 'foo' and 'bar' like we do it for 'id'. 
     } 
    } 
    } 
}) 

Как получить доступ к этим полям внутри других равных полей?

ответ

1

Любое поле должно быть разрешено изолировано.

const fooResolver = function(source, args, info) { 
    // Making some queries to database 
}; 
const barResolver = function(source, args, info) { 
    // Making some queries to database 
}; 
const bazResolver = function(source, args, info) { 
    const foo = fooResolver(source, args, info); 
    const bar = barResolver(source, args, info); 
    // TODO: Resolve baz. 
}; 

const MyType = new GraphQLObjectType({ 
    name 'MyType' 
    fields { 
    id: { 
     type: GraphQLString, // Resolved from parent type 
    }, 
    foo: { 
     type: GraphQLString, 
     description: 'Foo', 
     resolve: fooResolver, 
    }, 
    bar: { 
     type: GraphQLString, 
     description: 'Bar', 
     resolve: barResolver, 
    }, 
    baz: { 
     type: GraphQLString, 
     description: 'Baz', 
     resolve: bazResolver, 
    }, 
    }, 
}); 

Вы заметите, что это имеет плохие характеристики, если вы запрашиваете foo, baz и bar. Чтобы решить эту проблему, используйте загрузчик данных. Пример, с использованием facebook/dataloader:

function cacheKeyFn(input) { 
    // Produce a stable cache key string from the input 
} 
const fooLoader = new DataLoader(input => { 
    // Return a Promise that fetches `foo` given `input` 
}, {cacheKeyFn}); 
const barLoader = new DataLoader(input => { 
    // Return a Promise that fetches `bar` given `input` 
}, {cacheKeyFn}); 
const bazLoader = new DataLoader(input => { 
    // Return a Promise that fetches `baz` given `input` 
}, {cacheKeyFn}); 

const fooResolver = function(source, args, info) { 
    return await fooLoader.load(source, args, info); 
}; 
const barResolver = function(source, args, info) { 
    return await barLoader.load(source, args, info); 
}; 
const bazResolver = function(source, args, info) { 
    return await bazLoader.load(source, args, info); 
}; 
const bazResolver = function(source, args, info) { 
    const foo = await fooResolver(source, args, info); 
    const bar = await barResolver(source, args, info); 
    return await bazResolver({...source, foo, bar}, args, info); 
}; 

 Смежные вопросы

  • Нет связанных вопросов^_^