A method and device for building an API interface and querying data
By automatically generating the field parser corresponding to the structure corresponding to the GraphQL SCHEMA structure, the problem of decentralization of the SCHEMA data structure and field parser management is solved, and the development efficiency of the API interface and the scalability of the system.
Patent Information
- Application Number
- CN202411562506.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-05
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2044-11-05
AI Technical Summary
When building API interfaces based on the Apollo GraphQL framework, Schema data structures and field parser management are dispersed, resulting in structural inconsistencies, maintenance difficulties and lack of automation tools, increasing the complexity of development and maintenance.
Provide a method and device that can automatically generate a field parser that is closely corresponding to the GraphQL SCHEMA structure, build a parser object through a statement configuration, and dynamically import the JavaScript file to obtain the analyzer object, simplify the management and maintenance of the field parser Essence
It improves the development efficiency of the API interface, enhances the maintenance and scalability of the system, reduces the workload of developers in manual writing and maintenance, and optimizes resource allocation.
Smart Images

Figure CN119065645B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for constructing an API interface and querying data. Background Art
[0002] When building an API interface based on the ApollpGraphQL framework, API interface developers often face the problem of decentralized management of the Schema data structure and the field resolver. The Schema defines the data structure that can be queried, and the field resolver is used to fill in this data. Although splitting the Schema data structure and the field resolver according to business logic can reduce the coupling between software codes, this also brings challenges in management and maintenance.
[0003] The traditional Schema data structure and field resolver management methods have the following defects:
[0004] (1) Structural inconsistency. The schema can be a tree structure, but when implementing a custom type field resolver, it can only be a single-layer structure. This leads to structural inconsistency between the schema data structure and the field resolver, increasing the cost of analyzing the problem.
[0005] (2) Difficult to maintain. As the application scales up, manually managing and maintaining a large number of Schema data structures and field resolvers becomes complex and error-prone.
[0006] (3) Lack of automated tools. Existing tools and methods do not support automated generation and maintenance of parsers, which means developers need to spend a lot of time manually writing and adjusting parsers. Summary of the invention
[0007] In view of the above-mentioned defects or deficiencies in the prior art, the present invention provides an API interface construction and data query method and device, which can automatically generate a field parser that closely corresponds to the structure of the GraphQL Schema, thereby improving the development efficiency of the API interface. When adding new fields and parsers, there is no need to modify the core logic, thereby improving the scalability of the system.
[0008] One aspect of the present invention provides an API interface construction and data query method for a GraphQL server, comprising the following steps:
[0009] Create a JavaScript file with a GraphQL operation type as a suffix; receive the structural parameters of a declarative parser in the JavaScript file, and construct a parser object for parsing the request field according to the structural parameters of the declarative parser; wherein the structural parameters of the declarative parser match the predefined GraphQL Schema data structure, and the GraphQL Schema data structure includes fields and types of data; obtain a file path list of the JavaScript file through a GraphQL server, dynamically import the JavaScript file according to the file path list, and obtain a parser object to be used from the imported JavaScript file; pass the obtained parser object to be used to the parser configuration item in the GraphQL server configuration parameter; parse the query string sent by the client and convert it into an executable query document, if the grammatical structure of the query document conforms to the GraphQL specification, the query field exists, and the client has access rights, then obtain the corresponding data from the data source according to the field path specified in the query document; the GraphQL server parses the obtained data through the parser according to the field path specified in the query document, and returns the parsed data to the client.
[0010] Furthermore, the step of constructing a parser object for parsing request fields according to the structural parameters of the declarative parser includes: creating a parser initial object for storing parser functions in the JavaScript file; adding a default root operation object in the parser initial object, and adding a parser function of the operation field type in the default root operation object; recursively parsing the result object in the structural parameters of the declarative parser layer by layer, adding the parsed field type and parser function to the parser initial object, and obtaining the parser object.
[0011] Furthermore, the step of obtaining the parser object to be used from the imported JavaScript file includes: initializing a module list, dynamically obtaining the module object exported by the JavaScript file for storing the parser according to the file path list, traversing each parser object in the module object, replacing the default root operation object with the current root operation object, and adding it to the module list; converting the data in the initialized module list into the parser object to be used.
[0012] Furthermore, the step of converting the data in the initialized module list into a parser object to be used includes: traversing the initialized module list, adding the merged current root operation object and the custom type parsing object to the parser object, and obtaining the parser object to be used.
[0013] Furthermore, it also includes: data query includes security log query, and the GraphQL Schema data structure includes: security log type and field, security event type and field, and analysis rule type and field.
[0014] Another aspect of the present invention further provides an API interface construction and data query device for a GraphQL server, comprising:
[0015] The file generation module is configured to create a JavaScript file with a GraphQL operation type as a suffix; the parser construction module is configured to receive the structural parameters of the declarative parser in the JavaScript file, and construct a parser object for parsing the request field according to the structural parameters of the declarative parser; wherein the structural parameters of the declarative parser match the predefined GraphQL Schema data structure, and the GraphQL The Schema data structure includes fields and types of data; a parser loading module is configured to obtain a file path list of the JavaScript file through a GraphQL server, dynamically import the JavaScript file according to the file path list, and obtain a parser object to be used from the imported JavaScript file; the obtained parser object to be used is passed to the parser configuration item in the GraphQL server configuration parameters; a query module is configured to parse the query string sent by the client and convert it into an executable query document. If the grammatical structure of the query document conforms to the GraphQL specification, the query field exists, and the client has access rights, then the corresponding data is obtained from the data source according to the field path specified in the query document; a parsing module is configured for the GraphQL server to parse the acquired data through the parser according to the field path specified in the query document, and return the parsed data to the client.
[0016] Furthermore, the parser construction module is further configured to: create a parser initial object for storing parser functions in the JavaScript file; add a default root operation object in the parser initial object, and add a parser function of the operation field type in the default root operation object; recursively parse the result object in the structural parameters of the declarative parser layer by layer, add the parsed field type and parser function to the parser initial object, and obtain the parser object.
[0017] Furthermore, the parser loading module is further configured to: initialize a module list, dynamically obtain a module object exported by the JavaScript file for storing the parser according to a file path list, traverse each parser object in the module object, replace the default root operation object with the current root operation object, and add it to the module list; convert the data in the initialized module list into a parser object to be used.
[0018] Furthermore, the parser loading module is further configured to: traverse the initialized module list, add the merged current root operation object and the user-defined type parsing object to the parser object, and obtain the parser object to be used.
[0019] Furthermore, the data query includes a security log query, and the GraphQL Schema data structure includes: security log types and fields, security event types and fields, and analysis rule types and fields.
[0020] The present invention provides a method and device for building an API interface and querying data, and the specific effects are as follows:
[0021] (1) Improve the efficiency of API interface development. The traditional field parser management method requires developers to manually write and maintain a large amount of code, which is not only time-consuming but also prone to errors. The present invention can automatically generate field parsers, reducing the workload of developers in manual writing and maintenance.
[0022] (2) Enhance the maintainability of the API interface. The declarative configuration of the field resolver makes the generation of the field resolver closely correspond to the data structure of the GraphQL Schema, improving the readability and maintainability of the API interface software.
[0023] (3) Improve the scalability of the API interface. When new fields and parsers need to be added, the present invention allows developers to easily extend new fields and parsers through declarative configuration without modifying the core logic, which simplifies the extension process and improves the flexibility and scalability of the system.
[0024] (4) Optimize resource allocation. Developers can spend more time on optimizing and innovating business logic instead of writing and maintaining field resolvers, thereby making more efficient use of development resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] Other features, objects and advantages of the present application will become more apparent by reading the detailed description of non-limiting embodiments made with reference to the following drawings:
[0026] Figure 1This is a flowchart of an API interface construction and data query method provided by an embodiment of the present application;
[0027] Figure 2 This is a flowchart of constructing a parser in a JavaScript file provided by an embodiment of the present application;
[0028] Figure 3 This is a flowchart of a parser loading on a GraphQL server provided by an embodiment of the present application;
[0029] Figure 4 It is a schematic diagram of the structure of the API interface construction and data query device provided by one embodiment of the present application;
[0030] Figure 5 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0031] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0032] The terms used in the embodiments of the present invention are only for the purpose of describing specific embodiments, and are not intended to limit the present invention. The singular forms "a", "said" and "the" used in the embodiments of the present invention and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings.
[0033] It should be understood that although the terms first, second, third, etc. may be used to describe the acquisition modules in the embodiments of the present invention, the acquisition modules should not be limited to these terms. These terms are only used to distinguish the acquisition modules from each other.
[0034] The word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is detected" may be interpreted as "when it is determined" or "in response to determining" or "when detecting (stated condition or event)" or "in response to detecting (stated condition or event)", depending on the context.
[0035] It should be noted that the directional words such as "upper", "lower", "left", and "right" described in the embodiments of the present invention are described at the angles shown in the drawings and should not be understood as limiting the embodiments of the present invention. In addition, in the context, it should also be understood that when it is mentioned that an element is formed "on" or "under" another element, it can not only be formed directly "on" or "under" another element, but also be formed "on" or "under" another element indirectly through an intermediate element.
[0036] With the development of digital technology, API interfaces have become an indispensable part of applications and websites. Through API interfaces, technicians can easily obtain data, perform operations, or interact with other services. Among them, query API is a common type of API interface, which allows us to send requests to obtain specific data. GraphQL is a query language and operating environment for APIs, providing a more efficient, powerful and flexible way to query and manipulate data.
[0037] The following are some technical terms that appear in this application specification, and their concepts are as follows:
[0038] Field: is the basic unit in GraphQL query, which tells the GraphQL server what data you want to get. Fields can be nested, which means that you can request data from another field in one field.
[0039] Type: In GraphQL, types are used to define the structure of data. The types supported by GraphQL are scalar types, object types, interface types, union types, enumeration types, and input types.
[0040] Query: used to get data from the GraphQL server. In GraphQL, queries are done by defining fields that tell the GraphQL server what data you want to get.
[0041] Parser / Field Parser: It is a software function module or function module responsible for processing queries and mutations. They receive requests from clients, obtain data from the data source based on the fields in the request, and format it into the response format expected by the client.
[0042] Regular object: A regular object is a data structure in a JavaScript file that is used to store key-value pairs and manipulate data, such as a parser object.
[0043] Module object - is a special object used to implement software modularity and provide the ability to encapsulate, reuse, and maintain code, such as the parser module object.
[0044] GraphQL server-a program or service running on a GraphQL server. It is responsible for receiving and parsing GraphQL query requests sent by clients, performing corresponding data retrieval operations, and returning the results to the client.
[0045] In order to automatically generate a field parser that closely corresponds to the GraphQL Schema data structure, improve the development efficiency of the API interface, and enhance the scalability of the system, an embodiment of the present application provides an API interface construction and data query method. Figure 1 , the method comprises the following steps:
[0046] Step S101, creating a JavaScript file with a GraphQL operation type as a suffix.
[0047] Specifically, create a JavaScript file (i.e., JS file) with the GraphQL operation type as the suffix to export the resolver object returned by the genResolver function.
[0048] Step S102, receiving structural parameters of a declarative parser in a JavaScript file, and constructing a parser object for parsing request fields according to the structural parameters of the declarative parser; wherein the structural parameters of the declarative parser match a predefined GraphQL Schema data structure, and the GraphQL Schema data structure includes fields and types of data.
[0049] Specifically, in the JavaScript file, the genResolver function receives a declarative resolver structure parameter, which refers to a predefined GraphQL Schema data structure. Taking security log query as an example, the GraphQL Schema data structure includes security log types and fields, security event types and fields, and analysis rule types and fields.
[0050] See also Figure 2 , constructing a parser object for parsing request fields according to the structural parameters of the declarative parser, including:
[0051] Step S1021, creating a parser initial object for storing the parser function in the JavaScript file;
[0052] Step S1022, adding a default root operation object to the parser initial object, and adding a parser function of the operation field type to the default root operation object;
[0053] Step S1023, recursively parse the result object in the structure parameter of the declarative parser layer by layer, and add the parsed field type and parser function to the parser initial object;
[0054] Step S1024, complete the creation of the parser object and return the created parser object.
[0055] Step S103, obtaining a file path list of the JavaScript file through the GraphQL server, dynamically importing the JavaScript file according to the file path list, obtaining a parser object to be used from the imported JavaScript file; and passing the obtained parser object to be used to the parser configuration item in the GraphQL server configuration parameters.
[0056] Specifically, in the initialization phase of ApolloServer, the batchImport function is used to dynamically import the JavaScript file created above and generate a parser object. The ApolloServer is a GraphQL server program provided by the ApolloGraphQL framework.
[0057] See also Figure 3 , the process of loading a parser object includes the following steps:
[0058] Step S1031, read the created JavaScript file and generate a file path list;
[0059] Step S1032, dynamically importing the JavaScript file according to the file path list;
[0060] Step S1033, initializing the modules list (module list), dynamically obtaining the module object exported by the JavaScript file for storing the parser according to the file path list, traversing each parser object in the module object, replacing the default root operation object with the current root operation object, and adding it to the modules list;
[0061] Step S1034, traverse the initialized modules list, add the merged current root operation object and the user-defined type parsing object to the parser object, and obtain the parser object to be used.
[0062] Finally, pass the obtained resolver object to be used to the resolver configuration item in the GraphQL server configuration parameters.
[0063] Step S104, parse the query string sent by the client and convert it into an executable query document. If the grammatical structure of the query document conforms to the GraphQL specification, the query field exists, and the client has access rights, then obtain the corresponding data from the data source according to the field path specified in the query document.
[0064] Specifically, the GraphQL server parses the query string sent by the client and converts it into an executable query document. The GraphQL server verifies whether the syntax and structure of the query conform to the GraphQL specification, and checks whether the fields used in the query exist, whether the client has permission to access them, etc. Once the query passes the verification, the GraphQL server starts to execute the query. During the execution process, the server will obtain the corresponding data from the data source according to the field path defined in the query document.
[0065] Step S105, the GraphQL server parses the acquired data through a parser according to the field path specified in the query document, and returns the parsed data to the client.
[0066] Specifically, after obtaining the data, the GraphQL server will convert and assemble the data according to the field path specified in the query document to meet the data format requested by the client. The GraphQL server returns the processed data results to the client, and the client displays or further processes the data according to its own needs.
[0067] The API interface construction and data query method of this embodiment can automatically generate a field parser that closely corresponds to the structure of the GraphQL Schema, thereby improving the development efficiency of the API interface. When adding new fields and parsers, there is no need to modify the core logic, thereby improving the scalability of the system.
[0068] See also Figure 4 Another embodiment of the present invention further provides an API interface construction and data query device 200, including a file generation module 201, a parser construction module 202, a parser loading module 203, a query module 204 and a parsing module 205. The API interface construction and data query device 200 can execute the method steps in the above method embodiment.
[0069] The API interface construction and data query device 200 includes:
[0070] The file generation module 201 is configured to create a JavaScript file with a GraphQL operation type as a suffix;
[0071] A parser construction module 202 is configured to receive structural parameters of a declarative parser in a JavaScript file, and construct a parser object for parsing request fields according to the structural parameters of the declarative parser; wherein the structural parameters of the declarative parser match a predefined GraphQL Schema data structure, and the GraphQL Schema data structure includes fields and types of data;
[0072] The parser loading module 203 is configured to obtain a file path list of the JavaScript file through the GraphQL server, dynamically import the JavaScript file according to the file path list, obtain a parser object to be used from the imported JavaScript file; and pass the obtained parser object to be used to the parser configuration item in the GraphQL server configuration parameter;
[0073] The query module 204 is configured to parse the query string sent by the client and convert it into an executable query document. If the grammatical structure of the query document conforms to the GraphQL specification, the query field exists, and the client has access rights, then the corresponding data is obtained from the data source according to the field path specified in the query document;
[0074] The parsing module 205 is configured as a GraphQL server that parses the acquired data through a parser according to the field path specified in the query document, and returns the parsed data to the client.
[0075] Furthermore, the parser construction module 202 is further configured to:
[0076] Create a parser initialization object for storing parser functions in the JavaScript file;
[0077] Add a default root operation object to the parser initialization object, and add the parser function of the operation field type to the default root operation object;
[0078] The result object in the structural parameter of the declarative parser is parsed layer by layer in a recursive manner, and the parsed field type and parser function are added to the parser initial object to obtain the parser object.
[0079] Furthermore, the parser loading module 203 is further configured as follows:
[0080] Initialize the module list, dynamically obtain the module object exported by the JavaScript file and used to store the parser according to the file path list, traverse each parser object in the module object, replace the default root operation object with the current root operation object, and add it to the module list;
[0081] Convert the data in the initialized module list into a parser object to be used.
[0082] Furthermore, the parser loading module 203 is further configured as follows:
[0083] Traverse the initialized module list, add the merged current root operation object and custom type parsing object to the parser object, and obtain the parser object to be used.
[0084] It should be noted that the construction of the API interface and the data query device 200 provided in this embodiment correspond to the technical solutions that can be used to execute various method embodiments. The implementation principles and technical effects are similar to the methods and will not be repeated here.
[0085] See also Figure 5 Another embodiment of the present application further provides an electronic device 300 for implementing the API interface construction and data query method in the method embodiment. The electronic device 300 may include but is not limited to a GraphQL server. Figure 5 The electronic device 300 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0086] like Figure 5 As shown, the electronic device 300 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 301, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 302 or a program loaded from a storage device 308 to a random access memory (RAM) 303 to implement the method of the embodiment of the present invention. In the RAM 303, various programs and data required for the operation of the electronic device 300 are also stored. The processing device 301, the ROM 302, and the RAM 303 are connected to each other via a bus 304. An input / output (I / O) interface 305 is also connected to the bus 304.
[0087] Typically, the following devices may be connected to the I / O interface 305: an input device 306 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 307 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 308 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 309. The communication device 309 may allow the electronic device 300 to communicate with other devices wirelessly or by wire to exchange data. Figure 5 The electronic device 300 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0088] The above description is only a preferred embodiment of the present invention. Those skilled in the art should understand that the scope of disclosure involved in the present invention is not limited to the technical solution formed by a specific combination of the above technical features, but also should cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present invention (but not limited to) to form a technical solution.
Claims
1. A method for constructing an API interface and querying data, for a GraphQL server, characterized in that: The steps include: Create a JavaScript file with the GraphQL operation type as the suffix; Receiving structural parameters of a declarative parser in a JavaScript file, and constructing a parser object for parsing request fields according to the structural parameters of the declarative parser; wherein the structural parameters of the declarative parser match a predefined GraphQL Schema data structure, and the GraphQL Schema data structure includes fields and types of data; Obtaining a file path list of the JavaScript file through the GraphQL server, dynamically importing the JavaScript file according to the file path list, and obtaining a parser object to be used from the imported JavaScript file; passing the obtained parser object to be used to the parser configuration item in the GraphQL server configuration parameter; Parse the query string sent by the client and convert it into an executable query document. If the grammatical structure of the query document conforms to the GraphQL specification, the query field exists, and the client has access rights, then obtain the corresponding data from the data source according to the field path specified in the query document; The GraphQL server parses the acquired data through the parser according to the field path specified in the query document, and returns the parsed data to the client; The step of constructing a parser object for parsing request fields according to the structural parameters of the declarative parser includes: Create a parser initialization object for storing parser functions in the JavaScript file; Add a default root operation object to the parser initialization object, and add the parser function of the operation field type to the default root operation object; Recursively parsing the result object in the structure parameter of the declarative parser layer by layer, adding the parsed field type and parser function to the parser initial object to obtain the parser object; The step of obtaining a parser object to be used from the imported JavaScript file includes: Initialize the module list, dynamically obtain the module object exported by the JavaScript file and used to store the parser according to the file path list, traverse each parser object in the module object, replace the default root operation object with the current root operation object, and add it to the module list; Convert the data in the initialized module list into a parser object to be used.
2. According to claim 1, a method for constructing an API interface and querying data is characterized in that: The step of converting the data in the initialized module list into a parser object to be used comprises: Traverse the initialized module list, add the merged current root operation object and custom type parsing object to the parser object, and obtain the parser object to be used.
3. The method for constructing an API interface and querying data according to claim 1, characterized in that: The data query includes a security log query, and the GraphQL Schema data structure includes: security log types and fields, security event types and fields, and analysis rule types and fields.
4. An API interface construction and data query device for a GraphQL server, characterized in that: include: The file generation module is configured to create JavaScript files suffixed with the GraphQL operation type; A parser construction module is configured to receive a structure parameter of a declarative parser in a JavaScript file, and construct a parser object for parsing request fields according to the structure parameter of the declarative parser; wherein the structure parameter of the declarative parser matches a predefined GraphQL Schema data structure, and the GraphQL Schema data structure includes fields and types of data; A parser loading module is configured to obtain a file path list of the JavaScript file through the GraphQL server, dynamically import the JavaScript file according to the file path list, obtain a parser object to be used from the imported JavaScript file; and pass the obtained parser object to be used to the parser configuration item in the GraphQL server configuration parameter; The query module is configured to parse the query string sent by the client and convert it into an executable query document. If the grammatical structure of the query document conforms to the GraphQL specification, the query field exists, and the client has access rights, the corresponding data is obtained from the data source according to the field path specified in the query document; The parsing module is configured as a GraphQL server that parses the acquired data through a parser according to the field path specified in the query document and returns the parsed data to the client; Wherein, the parser construction module is further configured as follows: Create a parser initialization object for storing parser functions in the JavaScript file; Add a default root operation object to the parser initialization object, and add the parser function of the operation field type to the default root operation object; Recursively parsing the result object in the structure parameter of the declarative parser layer by layer, adding the parsed field type and parser function to the parser initial object to obtain the parser object; Wherein, the parser loading module is further configured as follows: Initialize the module list, dynamically obtain the module object exported by the JavaScript file and used to store the parser according to the file path list, traverse each parser object in the module object, replace the default root operation object with the current root operation object, and add it to the module list; Convert the data in the initialized module list into a parser object to be used.
5. The device for constructing an API interface and querying data according to claim 4, characterized in that: The parser loader module is further configured to: Traverse the initialized module list, add the merged current root operation object and custom type parsing object to the parser object, and obtain the parser object to be used.
6. The device for constructing an API interface and querying data according to claim 4, characterized in that: The data query includes a security log query, and the GraphQL Schema data structure includes: security log types and fields, security event types and fields, and analysis rule types and fields.
Citation Information
Patent Citations
GraphQL-based interface construction method and system, electronic equipment and storage medium
CN113741878A
API file conversion method and system and computer storage medium
CN117172214A