-
Notifications
You must be signed in to change notification settings - Fork 2k
Accept an object as the argument to graphql() #356
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
Comments
Couldn't agree more. Should be relatively easy work for a PR |
Yup! Question is – would it be acceptable to do this with a backward compat hook such that the old 0.4.x API (without |
I personally don't think it's worth complicating the code going forward to keep back-compatibility with 0.4.0. I'd prefer having a clean solution for future upgrades, but maybe that's just because I don't yet have anything in production using graphql. |
Another thing this proposal would let us do is pass an option to graphql which tells it to format errors with the default function. I find it quite inconvenient to have to format them myself now. |
To add to what was said above: at this point making the interface backwards-compatible would require making it backwards-compatible with both v0.5.* and v0.4.*, which frankly I don't think is even possible because the arguments are optional. If someone passes three arguments, there's no way of knowing whether that third argument should be interpreted as the root value or as the context. |
I'm all for changing api to options object |
I think this is a good idea, but am curious how it would work. Right now the first argument to |
I think the idea would be that this change would in fact be breaking (or maybe we can check for various properties on the object), but it would allow future changes like adding |
This should really be done otherwise we will keep breaking the API's |
After going through this issue, I would like to propose the solution below. type ObjectArgumentGraphql{
schema: GraphQLSchema,
requestString: string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string
} Then rename the first argument schema to options ( at the moment I don't find a real good name so if you have some idea, it is will be great if you can share it :) ). This argument will use the Union type system. They will have type GraphQLSchema or ObjectArgumentGraphql. export function graphql(
options: GraphQLSchema | ObjectArgumentGraphql ,
requestString: ? string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string
): Promise<GraphQLResult> {
if(typeof options === 'ObjectArgumentGraphql'){
let { schema, requestString, rootValue , contextValue , variableValues, operationName } = options;
return graph( schema, requestString, rootValue , contextValue , variableValues, operationName);
}
return graph( schema, requestString, rootValue , contextValue , variableValues, operationName);
}
function graph(
schema: GraphQLSchema ,
requestString: string,
rootValue?: mixed,
contextValue?: mixed,
variableValues?: ?{[key: string]: mixed},
operationName?: ?string
): Promise<GraphQLResult> {
return new Promise(resolve => {
const source = new Source(requestString || '', 'GraphQL request');
const documentAST = parse(source);
const validationErrors = validate(schema, documentAST);
if (validationErrors.length > 0) {
resolve({ errors: validationErrors });
} else {
resolve(
execute(
schema,
documentAST,
rootValue,
contextValue,
variableValues,
operationName
)
);
}
}).catch(error => {
return { errors: [ error ] };
});
} What do you think about it ? |
A sensible approach could be to make the change and when Implementing this change would be quite simple since the first argument is easily identifiable (i.e., graphql(
schema,
requestString,
rootValue,
contextValue,
variableValues,
operationName
); // console.warn(deprecationReason);
graphql({
schema,
requestString,
rootValue,
contextValue,
variableValues,
operationName
}); // 👍 It would be a very simple change and I'd be willing to make a PR if it's something @leebyron would consider merging. EDIT: As everybody else has been commenting, this change would be very good because of two main reasons: (1) There is no specific argument order meaning that the developer could skip optional arguments (or the implementation could be simplified if it allows for optional arguments) and (2) as new arguments are added to the |
IMO it'd make the most sense to just go ahead and break the API. It's what was done for |
As well as execute() and subscribe() Closes #356
As well as execute() and subscribe() Closes #356
As well as execute() and subscribe() Closes #356
This came up in the context of graphql/graphql-relay-js#74 (comment), but I think that given the number of arguments to
graphql()
: https://github.com/graphql/graphql-js/blob/v0.5.0/src/graphql.js#L43-L50, it would make more sense for it to take an object rather than a list of arguments.Among other things, this would make it easier to provide backward compatibility with the v0.4.x API, and also just make it less error-prone for those of us not using e.g. Flow to get all the arguments right.
The text was updated successfully, but these errors were encountered: