GraphQL: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(46 intermediate revisions by the same user not shown)
Line 1: Line 1:
{|
{|
| valign="top" |
| valign="top" |
'''Schema'''
<source lang="json">
<source lang="json">
type Project {
type Project {
Line 10: Line 11:


| valign="top" |
| valign="top" |
'''Query'''
<source lang="json">
<source lang="json">
{
{
Line 19: Line 21:


| valign="top" |
| valign="top" |
'''Results'''
<source lang="json">
<source lang="json">
{
{
Line 26: Line 29:
}
}
</source>
</source>
|}
==Scalar==
About the types you can assign to fields or to the return values of operations. Graphql Java supports the following types:
String                  Boolean                  Int
Float                  ID                      Long
Short                  Byte                    Float
BigDecimal              BigInteger
==Sencha Store==
{|
| valign="top" |
Ext/data/
  ├─ operation/
  │  ├─ Create    [POST  ]
  │  ├─ Destory    [DELETE]
  │  ├─ Operation
  │  ├─ Read      [GET  ]
  │  └─ Update    [PUT  ]
  │
  ├─ proxy/
  │  ├─ Ajax
  │  ├─ Proxy
  │  └─ Rest
| valign="top" |
  │
  ├─ reader/
  │  ├─ Array
  │  ├─ JSON
  │  ├─ Reader
  │  └─ XML
  │
  ├─ request/
  │  ├─ Ajax
  │  ├─ Base
  │  └─ Form
  │
| valign="top" |


  │
  ├─ writer/
  │  ├─ Array
  │  ├─ JSON
  │  └─ XML
  │
  │
 
Ext/grid/filters/
  ├─ filter
  └─ Filters
|}
|}
https://docs.sencha.com/extjs/6.2.0/classic/Ext.data.JsonStore.html
https://docs.sencha.com/extjs/6.2.0/classic/src/JsonStore.js.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.data.proxy.Rest.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Rest.js.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.data.proxy.Ajax.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Ajax.js-1.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.toolbar.Paging.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Paging.js.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.grid.filters.Filters.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Filters.js.html
==Custom Scalar==
<source lang="java">
@Component
public class DateScalarType extends GraphQLScalarType {
    public DateScalarType() {
        super("Date", "java.sql.Date", new Coercing<Date, String>() {
            @Override
            public String serialize(Object dataFetcherResult) throws CoercingSerializeException {
                if(dataFetcherResult != null && dataFetcherResult instanceof Date) {
                    Date sqlDate = (Date)dataFetcherResult;
                    java.util.Date date = new Date(sqlDate.getTime());
                    String isoDate = DateUtil.format(date, DateUtil.ISO_DATE);
                    return isoDate;
                }
                return null;
            }
            @Override
            public Date parseValue(Object input) throws CoercingParseValueException {
                if(input != null && input instanceof String) {
                    String isoDate = (String)input;
                    java.util.Date date = DateUtil.parse(isoDate, DateUtil.ISO_DATE);
                    Date sqlDate = (date != null) ? new Date(date.getTime()) : null;
                    return sqlDate;
                }
                return null;
            }
            @Override
            public Date parseLiteral(Object input) throws CoercingParseLiteralException {
                if(input != null && input instanceof StringValue) {
                    String isoDate = ((StringValue)input).getValue();
                    java.util.Date date = DateUtil.parse(isoDate, DateUtil.ISO_DATE);
                    Date sqlDate = (date != null) ? new Date(date.getTime()) : null;
                    return sqlDate;
                }
                return null;
            }
        });
    }
}
</source>
==Handle Lazy Loads==
<source lang="java">
import javax.servlet.Filter;
import org.springframework.context.annotation.Configuration
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
@Configuration
public class SpringGraqphQLConfig {
    /**
    * Register the {@link OpenEntityManagerInViewFilter} so that the
    * GraphQL-Servlet can handle lazy loads during execution.
    *
    * @return
    */
    @Bean
    public Filter OpenFilter() {
        return new OpenEntityManagerInViewFilter();
    }
}
</source>
==Convention==
'''1. Query:'''
  i  => searchUsers: [User]
  ii  => searchUsersByOffset(page: Int, size: Int): [User]
  iii => searchUser(id: String): User
  iv  => searchUserCount: Long
  v  => searchUserPageCount(size: Int): Long
'''2. Mutation:'''
  i  => insertUser(in: UserIn!): User!
  ii  => updateUser(id: String!, in: UserIn!): User!
  iii => deleteUser(id: String!): Boolean!


==References==
==References==
Line 45: Line 195:
| valign="top" |
| valign="top" |
* [https://stackoverflow.com/questions/45959234 Authentication in Spring Boot using GraphQL]
* [https://stackoverflow.com/questions/45959234 Authentication in Spring Boot using GraphQL]
* [https://medium.com/@oleg.ilyenko/graphql-namespaces-proposal-2a097ce81d2a GraphQL Namespaces Proposal]
* [https://stackoverflow.com/questions/28902374 Spring Boot Exception Handling]
* [https://stackoverflow.com/questions/54637757 Set a base  for the API Endpoint]
* [https://github.com/merapar/graphql-spring-boot-starter GraphQL Spring boot starter]
* [https://spectrum.chat/graphql-java Spectrum GraphQL Java]
* [https://spectrum.chat/graphql-java Spectrum GraphQL Java]
* [https://www.howtographql.com/graphql-java/5-authentication/ GraphQL Authentication]
* [https://www.howtographql.com/graphql-java/5-authentication/ GraphQL Authentication]
* [https://github.com/graphql-java-kickstart/graphql-spring-boot#enable-graphql-servlet Enable GraphQL Servlet]
* [https://github.com/graphql-java-kickstart/graphql-spring-boot GraphQL Java Kikstart]
* [https://www.baeldung.com/spring-boot-actuators Spring Boot Actuator]
| valign="top" |
* [https://stackoverflow.com/questions/48037601 <code>LazyInitializationException</code> with Spring Boot]
* [https://stackoverflow.com/questions/45516743 AJAX query with a variable by using Http POST]
* [https://www.pluralsight.com/guides/building-a-graphql-server-with-spring-boot Building a GraphQL Server with Spring Boot]
* [https://stackoverflow.com/questions/53852450 Separate Resolvers into Multiple Classes]
* [https://dimitr.im/graphql-mutations-spring GraphQL mutations with Spring Boot]
* [https://github.com/graphql-java/graphql-java-subscription-example Subscriptions over WebSockets]
* [https://stackoverflow.com/questions/46721015 AJAX query by using Http GET]
* [https://www.howtographql.com/graphql-java/6-more-mutations/ Custom DataTime scalar]
* [https://stackoverflow.com/questions/51325556 GraphQL & Spring Boot]
* [https://atheros.ai/blog/how-to-use-graphql-aliases GraphQL Query Aliases]
|}
----
{|
| valign="top" |
* [https://medium.com/@jono/cache-graphql-post-requests-with-service-worker-100a822a388a Cache GraphQL POST requests with Service Worker]
* [https://medium.com/codingthesmartway-com-blog/creating-a-graphql-server-with-node-js-and-express-f6dddc5320e1 A GraphQL Server With <code>Node.js</code> and <code>Express</code>]
* [https://a.kabachnik.info/offline-post-requests-via-service-worker-and-indexeddb.html Caching POST requests for offline use in a PWA]
* [https://www.robinwieruch.de/graphql-apollo-server-tutorial GraphQL with Apollo Server and Express]
* [https://pieroxy.net/blog/pages/lz-string/index.html lz-string: JavaScript compression, fast!]
* [https://codesandbox.io/s/yv004pqnq9?from-embed SpaceX GraphQL API Code Sendbox]
* [https://dexie.org/docs/Tutorial/Getting-started Dexie.js - A JS library for IndexedDB]
* [https://github.com/jonchenn/sw-graphql Service Worker GraphQL Example]
* [https://github.com/howtographql/graphql-java/blob/master/src/main/java/com/howtographql/hackernews/GraphQLEndpoint.java GraphQL Web Servlet Endpoint]
* [https://developers.google.com/web/tools/workbox/ Workbox]
| valign="top" |
* [https://github.com/graphql-java-kickstart/graphql-java-servlet/issues/144 Subscriptions using <code>graphql-java-servlet</code>]
* [https://blog.devgenius.io/graphql-with-spring-boot-starter-graphql-7b406998c0b5 GraphQL with spring-boot-starter-graphql]
* [https://github.com/graphql-java/graphql-java-subscription-example GraphQL Subscriptions over WebSockets]
* [https://dimitr.im/uploading-files-spring-graphql Uploading files with Spring and GraphQL]
* [https://docs.spring.io/spring-graphql/docs/current/reference/html/ Spring GraphQL Documentation]
* [https://vueschool.io/courses/learn-graphql-fundamentals Learn GraphQL Fundamentals]
* [https://github.com/spring-projects/spring-graphql/tree/main/samples Spring GraphQL Samples]
* [https://github.com/fiyazbinhasan/GraphQLCoreFromScratch GraphQL with .NET Core]
* [https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html Application Properties]
* [[WebSocket]]
| valign="top" |
* [[Spring Security]]
* [[Keycloak]]


|}
|}

Latest revision as of 23:49, 23 September 2022

Schema

type Project {
  name: String
  tagline: String
  contributors: [User]
}

Query

{
  project(name: "GraphQL") {
    tagline
  }
}

Results

{
  "project": {
    "tagline": "A query language for APIs"
  }
}

Scalar

About the types you can assign to fields or to the return values of operations. Graphql Java supports the following types:

String                  Boolean                  Int
Float                   ID                       Long
Short                   Byte                     Float
BigDecimal              BigInteger

Sencha Store

Ext/data/
 ├─ operation/
 │  ├─ Create     [POST  ]
 │  ├─ Destory    [DELETE]
 │  ├─ Operation
 │  ├─ Read       [GET   ]
 │  └─ Update     [PUT   ]
 │
 ├─ proxy/
 │  ├─ Ajax
 │  ├─ Proxy
 │  └─ Rest
 │
 ├─ reader/
 │  ├─ Array
 │  ├─ JSON
 │  ├─ Reader
 │  └─ XML
 │
 ├─ request/
 │  ├─ Ajax
 │  ├─ Base
 │  └─ Form
 │
 │
 ├─ writer/
 │  ├─ Array
 │  ├─ JSON
 │  └─ XML
 │
 │
 

Ext/grid/filters/
 ├─ filter
 └─ Filters
https://docs.sencha.com/extjs/6.2.0/classic/Ext.data.JsonStore.html
https://docs.sencha.com/extjs/6.2.0/classic/src/JsonStore.js.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.data.proxy.Rest.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Rest.js.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.data.proxy.Ajax.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Ajax.js-1.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.toolbar.Paging.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Paging.js.html
https://docs.sencha.com/extjs/6.2.0/classic/Ext.grid.filters.Filters.html
https://docs.sencha.com/extjs/6.2.0/classic/src/Filters.js.html

Custom Scalar

@Component
public class DateScalarType extends GraphQLScalarType {
    public DateScalarType() {
        super("Date", "java.sql.Date", new Coercing<Date, String>() {

            @Override
            public String serialize(Object dataFetcherResult) throws CoercingSerializeException {
                if(dataFetcherResult != null && dataFetcherResult instanceof Date) {
                    Date sqlDate = (Date)dataFetcherResult;
                    java.util.Date date = new Date(sqlDate.getTime());
                    String isoDate = DateUtil.format(date, DateUtil.ISO_DATE);
                    return isoDate;
                }
                return null;
            }

            @Override
            public Date parseValue(Object input) throws CoercingParseValueException {
                if(input != null && input instanceof String) {
                    String isoDate = (String)input;
                    java.util.Date date = DateUtil.parse(isoDate, DateUtil.ISO_DATE);
                    Date sqlDate = (date != null) ? new Date(date.getTime()) : null;
                    return sqlDate;
                }
                return null;
            }

            @Override
            public Date parseLiteral(Object input) throws CoercingParseLiteralException {
                if(input != null && input instanceof StringValue) {
                    String isoDate = ((StringValue)input).getValue();
                    java.util.Date date = DateUtil.parse(isoDate, DateUtil.ISO_DATE);
                    Date sqlDate = (date != null) ? new Date(date.getTime()) : null;
                    return sqlDate;
                }
                return null;
            }
        });
    }
}

Handle Lazy Loads

import javax.servlet.Filter;
import org.springframework.context.annotation.Configuration
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;

@Configuration
public class SpringGraqphQLConfig {

    /**
     * Register the {@link OpenEntityManagerInViewFilter} so that the
     * GraphQL-Servlet can handle lazy loads during execution.
     *
     * @return
     */
    @Bean
     public Filter OpenFilter() {
        return new OpenEntityManagerInViewFilter();
    }
}

Convention

1. Query:
  i   => searchUsers: [User]
  ii  => searchUsersByOffset(page: Int, size: Int): [User]
  iii => searchUser(id: String): User
  iv  => searchUserCount: Long
  v   => searchUserPageCount(size: Int): Long

2. Mutation:
  i   => insertUser(in: UserIn!): User!
  ii  => updateUser(id: String!, in: UserIn!): User!
  iii => deleteUser(id: String!): Boolean!

References