-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting selectionSet from fieldASTs with external fragment #96
Comments
I suppose the short answer is that you cannot directly get to a fragment from a field AST. There must also be a mapping of fragment name to the fragment AST. Could you help me understand how you're using the fields and in what context you need access to the fragment definitions? Is this from within a field resolve function? |
@leebyron thank you for the quick answer! Yes, but I'm using this from query resolve function, not field resolve. Currently I'm working on mapping fieldASTs to MongoDB projections. I've done with the Field and InlineFragment, now I want to get FragmentSpread working to complete common cases of GraphQL queries. |
Could you link me to the point in your code where this is being used? I'm afraid I don't know what you mean by "query resolve function" |
@leebyron consider this const todoQueries = {
todo: {
type: todoType,
args: {
_id: {
name: '_id',
type: new GraphQLNonNull(GraphQLString),
},
},
resolve: (root, { _id }, source, fieldASTs) => {
const projs = getProjection(fieldASTs);
return Todo.findById(_id, projs);
},
}
}; Queries here are part of GraphQL schema. And I'm working on function getProjection(fieldASTs) {
const { selections } = fieldASTs.selectionSet;
return selections.reduce((projs, selection) => {
switch (selection.kind) {
case 'Field':
return {
...projs,
[selection.name.value]: 1
};
case 'InlineFragment':
return {
...projs,
...getProjection(selection),
};
default:
throw 'Unsupported query';
}
}, {});
} |
@leebyron hi! The use case is that in graffiti-mongoose we are doing some kind of field selection (projection) with Mongoose to optimise our queries to get only the necessary data from the database. Our issue is that currently we cannot get the required fields from Details: Model.find({ age: 26 }, { name: 1, createdAt: 1 }) so the second parameter is the field selection. For example from this query:
we would like to generate the projection object below: {
name: 1,
foo: 1
} But currently we cannot read the selection fields from the selection if it's The current workaround is that if the query contains fragment we don't do any projection and get all of the fields from the database: Any idea how can we read the required fields from |
This is now solved by #119 which should be released on npm soon. |
@leebyron thanks! |
Hello, Sorry to reopen this ticket, but it seems that nobody kept going with this... I need the same thing, and here is the update of @gyzerok 's code to deal with the current version : export default function getFieldList(asts) {
//for recursion...Fragments don't have many sets...
if (!Array.isArray(asts)) asts = [asts]
//get all selectionSets
var selections = asts.reduce((selections, source) => {
selections.push(...source.selectionSet.selections);
return selections;
}, []);
//return fields
return selections.reduce((list, ast) => {
switch (ast.kind) {
case 'Field' :
list[ast.name.value] = true
return list;
case 'InlineFragment':
return {
...list,
...getFieldList(ast)
};
case 'FragmentSpread':
console.log(ast)
default:
throw new Error('Unsuported query selection')
}
}, {})
} It appears that Fieds are seen wherever they are declared, but selectionSets of FragmentSpread are not declared. log output : {
kind: 'FragmentSpread',
name: [Object],
directives: [],
loc: [Object]
} |
Ok, I got it... The above getFieldList function could work like this : export default function getFieldList(context, asts = context.fieldASTs) {
//for recursion...Fragments doesn't have many sets...
if (!Array.isArray(asts)) asts = [asts]
//get all selectionSets
var selections = asts.reduce((selections, source) => {
selections.push(...source.selectionSet.selections);
return selections;
}, []);
//return fields
return selections.reduce((list, ast) => {
switch (ast.kind) {
case 'Field' :
list[ast.name.value] = true
return list;
case 'InlineFragment':
return {
...list,
...getFieldList(context, ast)
};
case 'FragmentSpread':
return {
...list,
...getFieldList(context, context.fragments[ast.name.value])
};
default:
throw new Error('Unsuported query selection')
}
}, {})
} But in fact, it doesn't really get the child list because directives are ignored... They are solved later by GQL... I don't really know what to say about this... |
@Sandreu can you explain the above a bit more? I'm seeing this with 0.4.4 for the first example query in this thread: { kind: 'FragmentDefinition',
name:
{ kind: 'Name',
value: 'TextFragment',
loc: { start: 115, end: 127, source: [Object] } },
typeCondition:
{ kind: 'NamedType',
name: { kind: 'Name', value: 'Todo', loc: [Object] },
loc: { start: 131, end: 135, source: [Object] } },
directives: [],
selectionSet:
{ kind: 'SelectionSet',
selections: [ [Object] ],
loc: { start: 136, end: 150, source: [Object] } },
loc:
{ start: 106,
end: 150,
source:
Source {
body: <Buffer 20 71 75 65 72 79 20 51 75 65 72 79 57 69 74 68 46 72 61 67 6d 65 6e 74 20 7b 0a 20 20 20 20 74 6f 64 6f 28 5f 69 64 3a 20 22 35 35 61 36 32 34 62 61 ... >,
name: 'GraphQL' } } } and the selectionSet.selections from above: [ { kind: 'Field',
alias: null,
name: { kind: 'Name', value: 'text', loc: [Object] },
arguments: [],
directives: [],
selectionSet: null,
loc: { start: 142, end: 146, source: [Object] } } ] This looks like we have everything we need? |
Hello @parkan , I'm not quite sure that I'm getting your question right, Here is a complete example... import gql, {
graphql,
GraphQLString,
GraphQLSchema,
GraphQLObjectType,
} from './src';
var test = new GraphQLObjectType({
name: 'Test',
fields: () => ({
a : { type: GraphQLString, },
b : { type: GraphQLString, },
c : { type: GraphQLString, },
})
});
var Queries = new GraphQLObjectType({
name: 'Query',
fields: () => ({
req: {
type: test,
resolve: (_, inputs, context) => {
console.log(getFieldList(context));
/*********************************
{ a: true, b: true, c: true }
*********************************/
// Here is your request with fields selection !
return { a:'a', b:'b', c:'c'}
}
}
}),
});
var Schema = new GraphQLSchema({
query: Queries,
});
function getFieldList(context, asts = context.fieldASTs) {
//for recursion...Fragments doesn't have many sets...
if (!Array.isArray(asts)) asts = [asts]
//get all selectionSets
var selections = asts.reduce((selections, source) => {
selections.push(...source.selectionSet.selections);
return selections;
}, []);
//return fields
return selections.reduce((list, ast) => {
switch (ast.kind) {
case 'Field' :
list[ast.name.value] = true
return list;
case 'InlineFragment':
return {
...list,
...getFieldList(context, ast)
};
case 'FragmentSpread':
console.log(ast)
/**********************
{ kind: 'FragmentSpread',
name: [Object],
directives: [],
loc: [Object] }
*************************/
return {
...list,
...getFieldList(context, context.fragments[ast.name.value])
};
default:
throw new Error('Unsuported query selection')
}
}, {})
}
graphql(Schema, '{ req { a, ...f } } fragment f on Test { b, c }'); As you can see here, the I gave up on querying my db only the requested fields so I don't really know about corner cases with this graphql(Schema, '{ req { a, ...f @skip(if:true) } } fragment f on Test { b, c }') Here |
@Sandreu I am facing the same issue. Did you find the solution? |
@ansarizafar A solution about what ? Getting the requested fields works with this |
@Sandreu I made a library that expands on the example above and includes handling for skip and include directives: graphql-list-fields |
How can I get fields requested in external fragment from fieldASTs?
Consider following query
This query results in following, so there is no way to get those fields without directly parsing query string.
Now lets take a look in InlineFragment version
We can easily access requested fields for fragment in
selectionSet
I'm currently working on conversion fieldASTs to MongoDB projections. I've done with InlineFragment. Any thoughts?
The text was updated successfully, but these errors were encountered: