Skip to content

Commit

Permalink
fix: throw helpful error in case "query", "url", or "method" is used …
Browse files Browse the repository at this point in the history
…as query variable (#265)
  • Loading branch information
gr2m authored Mar 9, 2021
1 parent cc3ec32 commit 8e15940
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ const NON_VARIABLE_OPTIONS = [
"mediaType",
];

const FORBIDDEN_VARIABLE_OPTIONS = ["query", "method", "url"];

const GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/;

export function graphql<ResponseData = GraphQlQueryResponseData>(
request: typeof Request,
query: string | RequestParameters,
options?: RequestParameters
): Promise<ResponseData> {
if (typeof query === "string" && options && "query" in options) {
return Promise.reject(
new Error(`[@octokit/graphql] "query" cannot be used as variable name`)
);
if (options) {
if (typeof query === "string" && "query" in options) {
return Promise.reject(
new Error(`[@octokit/graphql] "query" cannot be used as variable name`)
);
}

for (const key in options) {
if (!FORBIDDEN_VARIABLE_OPTIONS.includes(key)) continue;

return Promise.reject(
new Error(`[@octokit/graphql] "${key}" cannot be used as variable name`)
);
}
}

const parsedOptions =
Expand Down
40 changes: 40 additions & 0 deletions test/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,44 @@ describe("graphql()", () => {
);
});
});

it("url variable (#264)", () => {
expect.assertions(1);

const query = `query GetCommitStatus($url: URI!) {
resource(url: $url) {
... on Commit {
status {
state
}
}
}
}`;

return graphql(query, {
url: "https://example.com",
}).catch((error) => {
expect(error.message).toEqual(
`[@octokit/graphql] "url" cannot be used as variable name`
);
});
});

it("method variable", () => {
expect.assertions(1);

const query = `query($method:String!){
search(query:$method,type:ISSUE) {
codeCount
}
}`;

return graphql(query, {
method: "test",
}).catch((error) => {
expect(error.message).toEqual(
`[@octokit/graphql] "method" cannot be used as variable name`
);
});
});
});

0 comments on commit 8e15940

Please sign in to comment.