Skip to content

Commit 1bc125f

Browse files
committed
Expose API to parse with experimental schema definition support
1 parent bc7533d commit 1bc125f

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

GraphQLParser.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,56 @@ namespace graphql {
2020

2121
// Given properly-configured yylex, run the parser and return the
2222
// result.
23-
static std::unique_ptr<ast::Node> doParse(const char **outError, yyscan_t scanner) {
23+
static std::unique_ptr<ast::Node> doParse(const char **outError, yyscan_t scanner, bool enableSchema) {
2424
Node *outAST;
25-
yy::GraphQLParserImpl parser(false, &outAST, outError, scanner);
25+
yy::GraphQLParserImpl parser(enableSchema, &outAST, outError, scanner);
2626
int failure = parser.parse();
2727
return !failure ? std::unique_ptr<ast::Node>(outAST) : nullptr;
2828
}
2929

30-
std::unique_ptr<ast::Node> parseString(const char *text, const char **error) {
30+
static std::unique_ptr<ast::Node> parseStringImpl(const char *text, const char **error, bool enableSchema) {
3131
yyscan_t scanner;
3232
struct LexerExtra extra;
3333
yylex_init_extra(&extra, &scanner);
3434
YY_BUFFER_STATE buffer = yy_scan_string(text, scanner);
3535
yy_switch_to_buffer(buffer, scanner);
3636

37-
auto result = doParse(error, scanner);
37+
auto result = doParse(error, scanner, enableSchema);
3838
yylex_destroy(scanner);
3939
return result;
4040
}
4141

42-
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error) {
42+
std::unique_ptr<ast::Node> parseString(const char *text, const char **error) {
43+
return parseStringImpl(text, error, false);
44+
}
45+
46+
std::unique_ptr<ast::Node> parseStringWithExperimentalSchemaSupport(
47+
const char *text, const char **error) {
48+
return parseStringImpl(text, error, true);
49+
}
50+
51+
static std::unique_ptr<ast::Node> parseFileImpl(
52+
FILE *file, const char **error, bool enableSchema) {
4353
yyscan_t scanner;
4454
struct LexerExtra extra;
4555
yylex_init_extra(&extra, &scanner);
4656
yyset_in(file, scanner);
4757

48-
auto result = doParse(error, scanner);
58+
auto result = doParse(error, scanner, enableSchema);
4959
yylex_destroy(scanner);
5060

5161
return result;
5262
}
5363

64+
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error) {
65+
return parseFileImpl(file, error, false);
66+
}
67+
68+
std::unique_ptr<ast::Node> parseFileWithExperimentalSchemaSupport(
69+
FILE *file, const char **error) {
70+
return parseFileImpl(file, error, true);
71+
}
72+
73+
5474
}
5575
}

GraphQLParser.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,26 @@ namespace ast {
3232
*/
3333
std::unique_ptr<ast::Node> parseString(const char *text, const char **error);
3434

35+
/**
36+
* Like parseString, but enables support for the experimental type
37+
* definition syntax from https://github.com/facebook/graphql/pull/90 .
38+
*/
39+
std::unique_ptr<ast::Node> parseStringWithExperimentalSchemaSupport(
40+
const char *text, const char **error);
41+
3542
/**
3643
* Read and parse GraphQL source from the given file, returning an
3744
* AST. Returns nullptr on error and, if error is not null, places an
3845
* error string in error that must be freed with free(3).
3946
*/
4047
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error);
4148

49+
/**
50+
* Like parseFile, but enables support for the experimental type
51+
* definition syntax from https://github.com/facebook/graphql/pull/90 .
52+
*/
53+
std::unique_ptr<ast::Node> parseFileWithExperimentalSchemaSupport(
54+
FILE *file, const char **error);
55+
4256
}
4357
}

c/GraphQLParser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ struct GraphQLAstNode *graphql_parse_string(const char *text, const char **error
1717
return (struct GraphQLAstNode *)facebook::graphql::parseString(text, error).release();
1818
}
1919

20+
struct GraphQLAstNode *graphql_parse_string_with_experimental_schema_support(
21+
const char *text, const char **error) {
22+
return (struct GraphQLAstNode *)facebook::graphql::parseStringWithExperimentalSchemaSupport(
23+
text, error).release();
24+
}
25+
2026
struct GraphQLAstNode *graphql_parse_file(FILE *file, const char **error) {
2127
return (struct GraphQLAstNode *)facebook::graphql::parseFile(file, error).release();
2228
}
2329

30+
struct GraphQLAstNode *graphql_parse_file_with_experimental_schema_support(
31+
FILE *file, const char **error) {
32+
return (struct GraphQLAstNode *)facebook::graphql::parseFileWithExperimentalSchemaSupport(file, error).release();
33+
}
34+
2435
void graphql_error_free(const char *error) {
2536
std::free((void *)error);
2637
}

c/GraphQLParser.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ struct GraphQLAstNode;
2828
* error message is placed in error and must be freed with
2929
* graphql_error_free().
3030
*/
31-
struct GraphQLAstNode *graphql_parse_string(const char *text,
32-
const char **error);
31+
struct GraphQLAstNode *graphql_parse_string(
32+
const char *text, const char **error);
33+
34+
struct GraphQLAstNode *graphql_parse_string_with_experimental_schema_support(
35+
const char *text, const char **error);
3336

3437
/**
3538
* Read and parse GraphQL source from the given file, returning an
@@ -40,6 +43,9 @@ struct GraphQLAstNode *graphql_parse_string(const char *text,
4043
*/
4144
struct GraphQLAstNode *graphql_parse_file(FILE *file, const char **error);
4245

46+
struct GraphQLAstNode *graphql_parse_file_with_experimental_schema_support(
47+
FILE *file, const char **error);
48+
4349
/**
4450
* Frees an error.
4551
*/

0 commit comments

Comments
 (0)