AppSync - query for all items created within a date range?












1















I'm trying to query my items (which have fields of AWS DateTime of CreatedAt and UpdatedAt) for all within a certain date range. For example, the past 48 hours.



For example, using this schema:



type Note @model @searchable @auth(rules: [{ allow: owner }]) {
id: ID!
note: String
createdAt: AWSDateTime


I'm able to search for dates using, for example:



query {
searchNotes(filter:{createdAt: { matchPhrasePrefix: "2018-12-27"}}) {
items{
id
title
createdAt
}
}
}


Which returns all notes that match the UTC time with that string prefix.



From which, I have to sort myself using moment.diff(), or some other method.



I'm not sure there is a better/more efficient way of doing searching/filtering by dates and time using AppSync and GraphQl?



Thank you.










share|improve this question



























    1















    I'm trying to query my items (which have fields of AWS DateTime of CreatedAt and UpdatedAt) for all within a certain date range. For example, the past 48 hours.



    For example, using this schema:



    type Note @model @searchable @auth(rules: [{ allow: owner }]) {
    id: ID!
    note: String
    createdAt: AWSDateTime


    I'm able to search for dates using, for example:



    query {
    searchNotes(filter:{createdAt: { matchPhrasePrefix: "2018-12-27"}}) {
    items{
    id
    title
    createdAt
    }
    }
    }


    Which returns all notes that match the UTC time with that string prefix.



    From which, I have to sort myself using moment.diff(), or some other method.



    I'm not sure there is a better/more efficient way of doing searching/filtering by dates and time using AppSync and GraphQl?



    Thank you.










    share|improve this question

























      1












      1








      1


      1






      I'm trying to query my items (which have fields of AWS DateTime of CreatedAt and UpdatedAt) for all within a certain date range. For example, the past 48 hours.



      For example, using this schema:



      type Note @model @searchable @auth(rules: [{ allow: owner }]) {
      id: ID!
      note: String
      createdAt: AWSDateTime


      I'm able to search for dates using, for example:



      query {
      searchNotes(filter:{createdAt: { matchPhrasePrefix: "2018-12-27"}}) {
      items{
      id
      title
      createdAt
      }
      }
      }


      Which returns all notes that match the UTC time with that string prefix.



      From which, I have to sort myself using moment.diff(), or some other method.



      I'm not sure there is a better/more efficient way of doing searching/filtering by dates and time using AppSync and GraphQl?



      Thank you.










      share|improve this question














      I'm trying to query my items (which have fields of AWS DateTime of CreatedAt and UpdatedAt) for all within a certain date range. For example, the past 48 hours.



      For example, using this schema:



      type Note @model @searchable @auth(rules: [{ allow: owner }]) {
      id: ID!
      note: String
      createdAt: AWSDateTime


      I'm able to search for dates using, for example:



      query {
      searchNotes(filter:{createdAt: { matchPhrasePrefix: "2018-12-27"}}) {
      items{
      id
      title
      createdAt
      }
      }
      }


      Which returns all notes that match the UTC time with that string prefix.



      From which, I have to sort myself using moment.diff(), or some other method.



      I'm not sure there is a better/more efficient way of doing searching/filtering by dates and time using AppSync and GraphQl?



      Thank you.







      elasticsearch graphql aws-appsync aws-amplify amplifyjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 9:20









      stephen lizcanostephen lizcano

      826




      826
























          1 Answer
          1






          active

          oldest

          votes


















          4














          As of writing (Jan 3, 2019), the easiest way to do this would be to store the date as an integer (e.g. seconds since epoch) which would open up the gt, lt, gte, ... filter fields on the auto-generated search resolvers filter input.



          Another solution is to write your own resolver using the AWS AppSync console or your own CloudFormation stack. When writing your own resolver you can leverage the entire Elasticsearch DSL to implement all kinds of queries (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html). To go down this route, you can add your own search field to the Query type in your schema and write a custom resolver.



          type Query {
          searchNotesByCreatedAt(start: String!, end: String!): NotesConnection
          }


          Then via the console or via your own CloudFormation stack you can write a resolver like this:



          {
          "version": "2017-02-28",
          "operation": "GET",
          "path": "/note-<your-api-id>/doc/_search", // created by amplify
          "params": {
          "body": {
          "sort": [{ "createdAt" : {"order" : "desc"}}],
          "query": {
          "range" : {
          "createdAt" : {
          "gte": $ctx.args.start,
          "lte": $ctx.args.end
          }
          }
          }
          }
          }
          }


          You will only need to deploy this resolver using the console or your own CF stack temporarily. Work is being done to allow you to write your own resolvers from within the amplify CLI. For more information on how this will work see https://github.com/aws-amplify/amplify-cli/issues/574.



          Let me know if this works for you.






          share|improve this answer
























          • This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

            – stephen lizcano
            Jan 4 at 7:48






          • 1





            Glad this worked out for you.

            – mparis
            Jan 4 at 8:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003825%2fappsync-query-for-all-items-created-within-a-date-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          As of writing (Jan 3, 2019), the easiest way to do this would be to store the date as an integer (e.g. seconds since epoch) which would open up the gt, lt, gte, ... filter fields on the auto-generated search resolvers filter input.



          Another solution is to write your own resolver using the AWS AppSync console or your own CloudFormation stack. When writing your own resolver you can leverage the entire Elasticsearch DSL to implement all kinds of queries (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html). To go down this route, you can add your own search field to the Query type in your schema and write a custom resolver.



          type Query {
          searchNotesByCreatedAt(start: String!, end: String!): NotesConnection
          }


          Then via the console or via your own CloudFormation stack you can write a resolver like this:



          {
          "version": "2017-02-28",
          "operation": "GET",
          "path": "/note-<your-api-id>/doc/_search", // created by amplify
          "params": {
          "body": {
          "sort": [{ "createdAt" : {"order" : "desc"}}],
          "query": {
          "range" : {
          "createdAt" : {
          "gte": $ctx.args.start,
          "lte": $ctx.args.end
          }
          }
          }
          }
          }
          }


          You will only need to deploy this resolver using the console or your own CF stack temporarily. Work is being done to allow you to write your own resolvers from within the amplify CLI. For more information on how this will work see https://github.com/aws-amplify/amplify-cli/issues/574.



          Let me know if this works for you.






          share|improve this answer
























          • This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

            – stephen lizcano
            Jan 4 at 7:48






          • 1





            Glad this worked out for you.

            – mparis
            Jan 4 at 8:40
















          4














          As of writing (Jan 3, 2019), the easiest way to do this would be to store the date as an integer (e.g. seconds since epoch) which would open up the gt, lt, gte, ... filter fields on the auto-generated search resolvers filter input.



          Another solution is to write your own resolver using the AWS AppSync console or your own CloudFormation stack. When writing your own resolver you can leverage the entire Elasticsearch DSL to implement all kinds of queries (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html). To go down this route, you can add your own search field to the Query type in your schema and write a custom resolver.



          type Query {
          searchNotesByCreatedAt(start: String!, end: String!): NotesConnection
          }


          Then via the console or via your own CloudFormation stack you can write a resolver like this:



          {
          "version": "2017-02-28",
          "operation": "GET",
          "path": "/note-<your-api-id>/doc/_search", // created by amplify
          "params": {
          "body": {
          "sort": [{ "createdAt" : {"order" : "desc"}}],
          "query": {
          "range" : {
          "createdAt" : {
          "gte": $ctx.args.start,
          "lte": $ctx.args.end
          }
          }
          }
          }
          }
          }


          You will only need to deploy this resolver using the console or your own CF stack temporarily. Work is being done to allow you to write your own resolvers from within the amplify CLI. For more information on how this will work see https://github.com/aws-amplify/amplify-cli/issues/574.



          Let me know if this works for you.






          share|improve this answer
























          • This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

            – stephen lizcano
            Jan 4 at 7:48






          • 1





            Glad this worked out for you.

            – mparis
            Jan 4 at 8:40














          4












          4








          4







          As of writing (Jan 3, 2019), the easiest way to do this would be to store the date as an integer (e.g. seconds since epoch) which would open up the gt, lt, gte, ... filter fields on the auto-generated search resolvers filter input.



          Another solution is to write your own resolver using the AWS AppSync console or your own CloudFormation stack. When writing your own resolver you can leverage the entire Elasticsearch DSL to implement all kinds of queries (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html). To go down this route, you can add your own search field to the Query type in your schema and write a custom resolver.



          type Query {
          searchNotesByCreatedAt(start: String!, end: String!): NotesConnection
          }


          Then via the console or via your own CloudFormation stack you can write a resolver like this:



          {
          "version": "2017-02-28",
          "operation": "GET",
          "path": "/note-<your-api-id>/doc/_search", // created by amplify
          "params": {
          "body": {
          "sort": [{ "createdAt" : {"order" : "desc"}}],
          "query": {
          "range" : {
          "createdAt" : {
          "gte": $ctx.args.start,
          "lte": $ctx.args.end
          }
          }
          }
          }
          }
          }


          You will only need to deploy this resolver using the console or your own CF stack temporarily. Work is being done to allow you to write your own resolvers from within the amplify CLI. For more information on how this will work see https://github.com/aws-amplify/amplify-cli/issues/574.



          Let me know if this works for you.






          share|improve this answer













          As of writing (Jan 3, 2019), the easiest way to do this would be to store the date as an integer (e.g. seconds since epoch) which would open up the gt, lt, gte, ... filter fields on the auto-generated search resolvers filter input.



          Another solution is to write your own resolver using the AWS AppSync console or your own CloudFormation stack. When writing your own resolver you can leverage the entire Elasticsearch DSL to implement all kinds of queries (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html). To go down this route, you can add your own search field to the Query type in your schema and write a custom resolver.



          type Query {
          searchNotesByCreatedAt(start: String!, end: String!): NotesConnection
          }


          Then via the console or via your own CloudFormation stack you can write a resolver like this:



          {
          "version": "2017-02-28",
          "operation": "GET",
          "path": "/note-<your-api-id>/doc/_search", // created by amplify
          "params": {
          "body": {
          "sort": [{ "createdAt" : {"order" : "desc"}}],
          "query": {
          "range" : {
          "createdAt" : {
          "gte": $ctx.args.start,
          "lte": $ctx.args.end
          }
          }
          }
          }
          }
          }


          You will only need to deploy this resolver using the console or your own CF stack temporarily. Work is being done to allow you to write your own resolvers from within the amplify CLI. For more information on how this will work see https://github.com/aws-amplify/amplify-cli/issues/574.



          Let me know if this works for you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 23:06









          mparismparis

          1,77187




          1,77187













          • This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

            – stephen lizcano
            Jan 4 at 7:48






          • 1





            Glad this worked out for you.

            – mparis
            Jan 4 at 8:40



















          • This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

            – stephen lizcano
            Jan 4 at 7:48






          • 1





            Glad this worked out for you.

            – mparis
            Jan 4 at 8:40

















          This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

          – stephen lizcano
          Jan 4 at 7:48





          This is a thorough, and very beneficial answer. Thank you! I actually did end up doing the first method you recommended, by storing the date in Unix Epoch time, which is an integer and sorting that way.

          – stephen lizcano
          Jan 4 at 7:48




          1




          1





          Glad this worked out for you.

          – mparis
          Jan 4 at 8:40





          Glad this worked out for you.

          – mparis
          Jan 4 at 8:40




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003825%2fappsync-query-for-all-items-created-within-a-date-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas